0

Compiler output files

 1 year ago
source link: https://maskray.me/blog/2023-04-25-compiler-output-files
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

For a GCC or Clang command, there is typically one primary output file, specified by -o or the default (a.out or a.exe). There can also be temporary files and auxiliary files.

Primary output file

We can specify the primary output file with the -o option. When unspecified, a default output file name is inferred from the input files and the final phase. When the final phase is linking, the default output file name is a.out or a.exe.

gcc -S d/a.c      # a.s
gcc -c d/a.c # a.o
gcc d/a.c # a.out
gcc d/a.c -o e/a # e/a

For -S and -c, specifying -o in the presence of more than one input files leads to an error.

% gcc -c d/a.c d/b.c -o e/x
gcc: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files
compilation terminated.

Temporary files

For compilation and linking in one command, the linker output file is the primary output file while the relocatable object files are temporary files.

For GCC, when generating a relocatable object file, it needs to generate a temporary assembly file, then feeds it to GNU assembler.

We can set the temporary directory with one of the environment variables TMPDIR, TMP, and TEMP. When none is specified, compilers have a fallback, /tmp on *NIX systems.

Auxiliary files

Beside primary output files and temporary output files, we have a third output file type called auxiliary output files. Some options such as -ftest-coverage and -gsplit-dwarf cause the compiler to generate auxiliary output files.

For compilation without linking (-c, -S, etc), the auxiliary output file names are derived from the primary output file name.

For compilation and linking in one command, the primary output file name affects the auxiliary output file names.

gcc -c -g -gsplit-dwarf d/a.c d/b.c      # a.o b.o a.dwo b.dwo
gcc -c -g -gsplit-dwarf d/a.c -o e/a.o # e/a.o e/a.dwo
gcc -g -gsplit-dwarf d/a.c d/b.c -o e/x # e/x (temporary .o files) e/x-a.dwo e/x-b.dwo
gcc -g -gsplit-dwarf d/a.c d/b.c # (temporary .o files) a-a.dwo a-b.dwo

# a.out is special cased.
gcc -g -gsplit-dwarf d/a.c.c -o e/x.out # e/x.out-a.dwo
gcc -g -gsplit-dwarf d/a.c.c -o e/a.out # e/a-a.dwo

GCC provides some options that are primarily of interest to GCC developers. These dump output files are treated the same way as auxiliary output files.

-dumpdir and -dumpbase are provided to control the auxiliary output file names. The official documentation may be difficult to follow. Let's see some examples.

gcc -g -gsplit-dwarf -dumpdir f d/a.c -c # fa.dwo
gcc -g -gsplit-dwarf -dumpdir f d/a.c # fa.dwo
gcc -g -gsplit-dwarf -dumpdir f/ d/a.c # f/a.dwo
gcc -g -gsplit-dwarf -dumpbase f d/a.c # f-a.dwo
gcc -g -gsplit-dwarf -dumpbase f/ d/a.c # f/-a.dwo

In the absence of -dumpdir, -dumpbase appends a dash, which makes it inconvenient.

If we specify both -dumpdir and -dumpbase, we can avoid the influence of the source filename when there is one input file.

gcc -g -gsplit-dwarf -dumpdir f/ -dumpbase x d/a.c        # f/x.dwo
gcc -g -gsplit-dwarf -dumpdir f/ -dumpbase x d/a.c d/b.c # f/x-a.dwo f/x-b.dwo

I suggest that you only use -dumpdir. When only -dumpdir is used, the behavior is still easy to explain.

I have a patch to support -dumpdir in Clang: https://reviews.llvm.org/D149193.

-save-temps

-save-temps and -save-temps={cwd,obj} generate intermediate files, which are treated as auxiliary output files in GCC.

gcc -save-temps d/a.c
ls a.i a.s a.o

In the absence of -dumpdir/-dumpbase, -save-temps=cwd places intermediate files in the current directory while -save-temps=obj places intermediate files in the directory of the primary output file. -save-temps behaves like -save-temps=obj when -o is specified, and -save-temps=cwd otherwise.

When -dumpdir is specified, there is complex interaction between -dumpdir and -save-temps/-save-temps={cwd,obj}.

# The last of -dumpdir and -save-temps wins.
gcc -g -gsplit-dwarf d/a.c -o e/x -dumpdir f/ -save-temps=obj # e/a.{i,s,o,dwo}
gcc -g -gsplit-dwarf d/a.c -o e/x -save-temps=obj -dumpdir f/ # f/a.{i,s,o,dwo}

For Clang, I think we should abandon the idea to treat these intermdiate files (*.i, *.bc, *.s, etc) as auxiliary output files. Just make -dumpdir and -save-temps/-save-temps={cwd,obj} orthogonal.

clang -g -gsplit-dwarf d/a.c -o e/x -save-temps=obj -dumpdir f/ # e/a.{i,s,o} f/a.dwo

Other auxiliary files

Clang supports a few options (e.g. -ftime-trace) to generate other auxiliary output files. I plan to change their file names to be controlled by -dumpdir.

Offloading

Clang supports offloading to various architectures using programming models like CUDA, HIP, and OpenMP. Using offloading options may generate multiple output files.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK