Specifying both -O2 and -O3 at same time - gcc

Some programs already uses -O2 flag, if I use -O3 flag, the program compiles with both -O2 and -O3 as shown by the task manager or by /proc/PID/cmdline.
For example, I'm using a Linux kernel built with Clang and full LTO. Even though I have these lines in the dkms configuration:
# /etc/dkms/framework.conf
export LLVM=1
export CC=clang
export CFLAGS="-O3 -march=native"
Now DKMS modules compile with both -O2 and -O3 flags. In this case, which flag is actually used?

For gcc, see https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Optimize-Options.html#Optimize-Options
If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
clang doesn't document its option syntax as extensively, but it generally tries to be compatible with gcc, so it should be the same in this regard.

Related

How to compile kernel with debug symbols for my own OS?

Hi I'm a beginner and trying to write a Linux like kernel.
I use Qemu as my emulator and currently debug in a assembly level.
However, by previous experience, I can debug Linux kernel with Qemu at source code (.c files) level.
So I would like to ask if I can do it with my own kernel, so that I can debug it with efficiency.
In order to provide more info, the following is my compilation script:
# Complie head.S
gcc -E ./PysicCodes/head.S > head.s
as --64 -o head.o head.s
gcc -E ./PysicCodes/AP_Boot.S > AP_Boot.s
as --64 -o AP_Boot.o AP_Boot.s
# Compile main program
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -c ./PysicCodes/*.c
# Interrupt hander requires general register only(since no XMM,SEE registers are saved)
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -mgeneral-regs-only -c ./PysicCodes/g_reg_only/*.c
# Linkage: Must put head.o at first, so that kernel start at head.o
ld -b elf64-x86-64 -z muldefs -o system head.o 8529A.o ACPI.o AP_Boot.o APIC.o cpu.o INT.o keyboard.o main.o Mem.o PCI.o Printk.o SMP.o Task.o Time.o TSS.o fat32.o -T ./PysicCodes/Kernel.lds
# Dump kernel
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system Kernel.bin
Thanks for anyone who would spend time on helping. Any extra information needed, please comment.
Currently I tried to add -ggdb3 as the compiling and linking options.
Furthermore, using the compiled object as the option for gdb
It simply worked.
In addition, adding "miDebuggerServerAddress": "localhost:1234" into Vscode's "launch.json" file, it actually can connect to Qemu and debug c code in vscode.
However only 1 problem, that vscode will run Qemu at start, so I have to press pause button as soon as I can after start debugger, and using "-exec" to put a hardware break-point.
There is an issue on git and currently haven't see an answer.

How can I automagically obtain a detailed script equivalent to a complex `gcc` invocation?

It is my understanding that, under the hood, a simple gcc invocation such as this:
% gcc -o hello hello.c
— May actually invoke several separate executables, perhaps hidden inside gcc installation. These may be:
The linker ld.
The assembler as.
An obscure executable cc1 that is actually a compiler.
An obscure executable collect2 with functionality that I find difficult to summarize.
Any number of other commands.
All of them will be invoked with an outrageous amount of command line parameters and environment variables. However, it is my understanding that the gcc executable does nothing by itself, that is, the whole run of gcc is completely described by the commands it runs, so any single invocation of gcc is equivalent to some shell script.
It is sometimes desirable to locate individual commands performed during a run of gcc, either to alter and perform them separately, trace a bug in the build process, or simply to document the particulars of a build. Furthermore, it is sometimes demanded that such effort is performed across several build configurations, target architectures, optimization parameters and so on.
A log of operation may be obtained from gcc by supplying a parameter -v, and redirecting to a file:
% gcc -o hello hello.c 2> gcc.log
Unfortunately, this method by itself does not provide a script that can readily be executed, altered, version controlled and so on. Rather, the log generated will contain a mixture of actual commands and arbitrary commentary, such as gcc version, all in a uniform list. It is then on the operator to manually mark the commentary as such or remove it altogether, in order to, hopefully, obtain a runnable shell script.
How can I (make ghc to) automagically generate such a script?
First of all note that command-line invocations alone are not sufficient - GCC passes additional options via environment variables (COMPILER_PATH, COLLECT_GCC_OPTIONS, etc.) and via temp files which contain compiler options inside them (the latter is AFAIK only used in LTO compilations).
You can easily extract compilation commands via sed:
$ gcc tmp.c -### 2>&1
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: ...
Thread model: posix
gcc version 6.4.0 (GCC)
COLLECT_GCC_OPTIONS='-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/cc1.exe -quiet -Dunix -idirafter /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../lib/../include/w32api -idirafter /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/lib/../lib/../../include/w32api tmp.c -quiet -dumpbase tmp.c "-mtune=generic" "-march=x86-64" -auxbase tmp -o /tmp/cco2cExb.s
...
$ gcc tmp.c -### 2>&1 | sed -ne '/^[A-Z_0-9]\+=/{ s/^\([^=]\+\)=\(.*\)/export \1="\2"/; s/'\''//g; p}; /^ /{p}'
export COLLECT_GCC="gcc"
export COLLECT_LTO_WRAPPER="/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/lto-wrapper.exe"
export COLLECT_GCC_OPTIONS="-mtune=generic -march=x86-64"
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/cc1.exe -quiet -Dunix -idirafter /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../lib/../include/w32api -idirafter /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/lib/../lib/../../include/w32api tmp.c -quiet -dumpbase tmp.c "-mtune=generic" "-march=x86-64" -auxbase tmp -o /tmp/ccZSUbZx.s
...

gccfilter and gcc 4.7.2 does not work, stops compiling

I have successfully installed the gccfilter (http://www.mixtion.org/gccfilter/) in my toolchain. The filter should actually work because all perl modules and other stuff has been installed the problem is that it does not work properly when I use for example the following command line:
gccfilter -c -a g++ -std=c++11 -O3 -DNDEBUG -I/"tonnes of includes" -o CMakeFiles/...../main.cpp.o
-c /...path.../App/main.cpp
.../variant.hpp:17:0,
from .../SceneParser.hpp:12,
from .../SimulationManager.hpp:12,
from .../main.cpp:8:
_ <-- Cursor is here
It compiles but after the error message it stops doing anything, the cursor is on the bottom line and nothing happens?
So the tool does not quite work, I am using gcc 4.7.2. i am not quite sure where the problem might be?

Mixed language C++, C and Fortran compilation with Cmake

Using g++, gcc and gfortran on GNU/Linux, I've written a simple script to compile and link together a number of source code files written in C++, C and Fortran. Here are the complete contents of the script. This script has been tested, and works well.
g++ -c test-Q.cpp -I./boost/boost_1_52_0/ -g
gcc -c paul2.c -g
gcc -c paul2_L1.c -g
gcc -c paul6.c -g
gcc -c paul6_L1.c -g
gcc -c fit_slope.c -g
gfortran -c getqpf.F -g
g++ -o test-Q test-Q.o paul2.o paul2_L1.o paul6.o paul6_L1.o fit_slope.o getqpf.o -g -lgfortran
To make this more cross-platform, I would like to re-write the script using Cmake. How might I handle mixed-language compilation?
The following test script listed below does not work, and will only selectively compile some of the files.
Is there perhaps another cross-platform build process that might be better suited for this type of compilation?
cmake_minimum_required (VERSION 2.6)
project (q-test)
include_directories(/media/RESEARCH/SAS2-version2/test-Q/boost/boost_1_52_0)
add_executable( q-test
test-Q.cpp
paul2.c
paul2_L1.c
paul6.c
paul6_L1.c
fit_slope.c
getqpf.F
) # end
You need to enable Fortran for the project like this:
project (q-test C CXX Fortran)
Also, you might want to use find_package(Boost) instead of hard coding an include path.

snapshot of memory for process on mac?

I want to take a snapshot of memory of process in action on mac. I have no idea how to do it.
I have IDA-PRO for mac with me. Can it be used? How?
Can anyone suggest me a way to do this? (some documentation or example).
May be some techniques from uni can be used but I am also not aware of that.
I dont want to kill the process as I want to see whats changing after execution of instructions/commands.
You can send a signal to a running process to dump core into a file, which can be used with gdb later for postmortem analysis.
kill -ABRT <process-id>
It seems that you must configure your system to enable core dump. See http://developer.apple.com/library/mac/#technotes/tn2124/_index.html for details.
UPDATE:
Well, above link introduces a third party implementation of gcore, a command line tool to make a core dump of running processes:
http://www.osxbook.com/book/bonus/chapter8/core/
You may just want to grab the source and try:
http://www.osxbook.com/book/bonus/chapter8/core/download/gcore-1.3.tar.gz
To make a single FAT binary to use with ppc/i386/x86_64, just modify following lines from Makefile:
gcore: gcore.c
gcc -O2 -arch ppc -arch i386 -Wall -o $# $<
as:
gcore: gcore.c
gcc -O2 -arch ppc -arch i386 -arch x86_64 -Wall -o $# $<

Resources