gcc / (g)as ignore breakpoint trap (x86) - gcc

I've written a C program which includes some assembler code in which I have some instructions which lead to a breakpoint exception (INT3) This is nice when debugging (since you don't have to save and restore the breakpoints when restarting the gdb session).
But now I want to disable these traps, which is possible by the -no-break or the -no-trap option of as (the gnu assembler), but I don't find a way to specify these options as options of gcc so that gcc passes the option down to as.
Is there a way to do so? (somehow gcc -g -o file -Xassembler -no-trap main.c file.S does not work)

Related

Automating GCC Compiler arguments to create easier compilations - Windows OS?

Goal
When I run the command:
gcc -ggdb -std=c99 -Wall -Werror hello.c -lcs50 -o test.exe from the root directory
I am able to build the test.exe file and when I run test.exe all is well (thanks to this post by Manohar Reddy Poreddy)
However all of those flags are a little bit cumbersome and I think it would great to condense them into a 'make' command or similar. How would I do this on windows?
Context
GCC, G++ and GDB all seem to be correctly linked (I used chocolatey which paths everything automatically)
Okay so I found what I was looking for.
I hope this answer can help others. Turns out the utility is called 'make' (no surprises). In your directory you essentially create a 'makefile' where you can include your command line arguments which saves on repeated typing in the command line for each compile.
Here is an excellent response on how to install 'make' for windows and was perfect for my use case as a Chocolatey user.
I also found this resource which helps newcomers begin to get their head round GCC which I highly recommend if you're coming into this like I was and felt completely out of your depth.

How can i fix MinGW -Wl, --dynamicbase not working properly?

I just read about ASLR, and i found gcc have related flag for ASLR from here. The flag is -Wl,--dynamicbase, so i try it with this command gcc test.c -Wl,--dynamicbase -o test.exe. I try run it and everything runs well, but when i check for ASLR with ProcessExplorer, it looks like ASLR for my program is turned off. I continue reading and find this flag -pie -fPIE, then i try again with this command gcc test.c -Wl,--dynamicbase -o test.exe -pie -fPIE, but after i run, the program receive SIGSEGV. I don't know for sure what is the problem. So could you give me the right flag or what i miss? My computer support for ASLR, i know it because ProcessExplorer show some process with ASLR turned on. Thanks for your attention.
test.c
#include <stdio.h>
int main(){
puts("lol");
}

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
...

How to prevent clang from turning "#warning ..." statement into error when -Werror is used?

Gcc has a compiler option -Wno-error=cpp which prevents compiler from turning "#warning ..." into error. However, clang doesn't support this option. Does clang have similar option?
Clang option -Wno-error=\#warnings is equivalent of gcc's -Wno-error=cpp. Escape \ is needed in Linux. Verified with clang-3.8 in Linux.

gcc -print-prog-name=??? doesn't work as I would expect

if I understand the gcc manuals right than the option -print-prog-name should print the name of the program used.
But it seems that this option only echoes the given argument
Examples:
gcc -print-prog-name=ld
--> ld
gcc -print-prog-name=xxxsome-funny-name
--> xxxsome-funny-name
Is this the expected behaviour? I think it should print something like
gcc -print-prog-name=ld
--> /usr/bin/ld
gcc -print-prog-name=xxxsome-funny-name
--> unknown program
EDIT: testing on Debian Lenny 64bit with gcc v4.2.4
Meanwhile I found another reason for the behaviour of
gcc -print-prog-name=ld
The ld command is not invoked directly by gcc.
gcc invokes collect. And it is collect which in turn invokes ld.
I think the -print-prog-name option only applies to a small set of tools that GCC
uses internally. For example,
$ gcc -print-prog-name=cc1
/usr/libexec/gcc/x86_64-redhat-linux/3.4.5/cc1
$ ls -L /usr/libexec/gcc/x86_64-redhat-linux/3.4.5/
cc1 cc1plus collect2 f771 jc1 jvgenmain
$ gcc -print-prog-name=f771
/usr/libexec/gcc/x86_64-redhat-linux/3.4.5/f771
So gcc -print-prog-name is aware of the tools that live in that directory. But:
$ gcc -print-prog-name=ld
ld
My guess is that if gcc -print-prog-name returns an absolute path, it's configured
to use that version of the program, no matter what's on your $PATH -- otherwise
it just echoes back what you gave it without resolving it to an absolute pathname.

Resources