40
Young Won Lim 9/29/14 Compiler (1A)

Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Young Won Lim9/29/14

Compiler (1A)

Page 2: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Young Won Lim9/29/14

Copyright (c) 2010-2013 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to [email protected].

This document was produced by using OpenOffice.

Page 3: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 3 Young Won Lim9/29/14

GNU Compiler

● cpp0 pre processor

● cc1 c compiler

● cc1obj objective-c compiler

● cc1plus c++ compiler

● f771 fortran compiler

● jc1 java compiler

● collect2 linker

Page 4: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 4 Young Won Lim9/29/14

GCC Compile Process

t.c

t.i

t.s

t.o

a.out

cpp0

cc1

as

ld / collect2

Page 5: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 5 Young Won Lim9/29/14

gcc

1. Program Filesc++, c++filt, cc (link to gcc), cc1, cc1plus, collect2, cpp, cpp0, g++, gcc, gccbug, gcov and tradcpp0

2. Descriptions

2.1 cc, cc1, cc1plus, gccThese are the C compiler.

2.2 c++, cc1plus, g++These are the C++ compiler, the equivalent of cc and gcc etc.

2.3 c++filtThe c++filt program does the inverse mapping: it decodes (demangles) low-level names into user-level names so that the linker can keep these overloaded functions from clashing.

2.4 collect2collect2 assists with the compilation of constructors.

The C++ language provides function overloading, which means that it is possible to write many functions with the same name (providing each takes parameters of different types). All C++ function names are encoded into a low-level assembly label (this process is known as mangling).

http://www.faqs.org/docs/linux_scratch/appendixa/gcc.html

Page 6: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 6 Young Won Lim9/29/14

gcc

2.5 cpp, cpp0cpp pre-processes a source file, such as including the contents of header files into the source file. Simply add a line, such as #include <filename>, to your source file. The preprocessor will insert the contents of the included file into the source file.

2.6 gccbuggccbug is a shell script which is used to simplify the creation of bug reports.

2.7 gcovgcov analyzes programs to help create more efficient, faster running code through optimization.

2.8 tradcpp0No description is currently available.

http://www.faqs.org/docs/linux_scratch/appendixa/gcc.htmlhttp://www.faqs.org/docs/linux_scratch/appendixa/gcc.html

Page 7: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 7 Young Won Lim9/29/14

cpp0

● gcc -v –save-temps -o test test.c

Page 8: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 8 Young Won Lim9/29/14

cc1

● gcc -da t.c

Page 9: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 9 Young Won Lim9/29/14

as

● as -V -Qy -o t t.s

Page 10: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 10 Young Won Lim9/29/14

collect2

Static Library

● mkdir libttt

● cd libttt

● cp /usr/lib/libc.a ./

● ar -x libttt.a

Dynamic Library

Page 11: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 11 Young Won Lim9/29/14

Compiling

gcc -Wall hello.c -o hello

gcc main.o hello_fn.o -o hello

gcc -Wall calc.c /usr/lib/libm.a -o calc

gcc -Wall calc.c -lm -o calc

gcc -Wall -lm calc.c -o calc (incorrect order)

gcc -Wall -I/opt/gdbm-1.8.3/include dbmain.c -lgdbm

gcc -Wall -I/opt/gdbm-1.8.3/include -L/opt/gdbm-1.8.3/lib dbmain.c -lgdbm

/usr/lib/libm.a

Page 12: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 12 Young Won Lim9/29/14

Compiling

$ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include $ export C_INCLUDE_PATH

$ CPLUS_INCLUDE_PATH=/opt/gdbm-1.8.3/include $ export CPLUS_INCLUDE_PATH

$ LIBRARY_PATH=/opt/gdbm-1.8.3/lib$ export LIBRARY_PATH

$ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include$ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

$ gcc -I. -I/opt/gdbm-1.8.3/include -I/net/include -L. -L/opt/gdbm-1.8.3/lib -L/net/lib .....

Page 13: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 13 Young Won Lim9/29/14

Gcc options

-E-S-c-v-- save-temps

Page 14: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 14 Young Won Lim9/29/14

cpp0 options

● -I

● -include

● -D

● -D[macro]=[macro val]

● -U[macro]

● -M

● -MM

● -nostdinc

● -C

● -Wp,[options]

Page 15: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 15 Young Won Lim9/29/14

cc1 options (1)

● -ansi

● -std=[c89, c99, gnu89, gnu99]

● -traditional

● -fno-asm

● -W, -Wall

● -w

● -Werror

● -pedantic

● -pedantic-errors

● -Wtraditional

Page 16: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 16 Young Won Lim9/29/14

cc1 options (2)

● -O0

● -O1

● -O2

● -O3

● -Os

● -g

● -pg

Page 17: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 17 Young Won Lim9/29/14

as options

● -Wa,[as options]

● -al

● -as

● -l[pass]

● -W, --no-warn

● -march=[arch string]

Page 18: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 18 Young Won Lim9/29/14

collect2 options (1)

● -L[library directory]

● -l[library name]

● -shared

● -static

● -nostdlib

● -nostartfiles

Page 19: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 19 Young Won Lim9/29/14

collect2 options (2)

● -W,[link options]

● -s

● -x

● -n

● -r

● -e [name]

● -oformat [format]

Page 20: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 20 Young Won Lim9/29/14

ELF

gcc -g t t.c

objdump -S t

gcc -v –save-temps -o t t.c

t.i (the preprocessed output)t.s (the assembly file)

Page 21: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 21 Young Won Lim9/29/14

-save-temps

-save-temps -save-temps=cwd Store the usual "temporary" intermediate files permanently; gcc -c -save-temps foo.c produces foo.i, foo.s, foo.o.

This creates a preprocessed foo.i output file even though the compiler now normally uses an integrated preprocessor.

Page 22: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 22 Young Won Lim9/29/14

objdump

objdump [ -a | --archive-headers ] [ -b bfdname | --target=bfdname ] [ -d | --disassemble ] [ -D | --disassemble-all ] [ -f | --file-headers ] [ -h | --section-headers | --headers ] [ -i | --info ] [ -j section | --section=section ] [ -l | --line-numbers ] [ -m machine | --architecture=machine ] [ -r | --reloc ] [ -R | --dynamic-reloc ] [ -s | --full-contents ] [ --stabs ] [ -t | --syms ] [ -T | --dynamic-syms ] [ -x | --all-headers ] [ --version ] [ --help ] objfile...

objdump displays information about one or more object files. The options control what particular information to display. This information is mostly useful to programmers who are working on the compilation tools, as opposed to programmers who just want their program to compile and work.

objfile... are the object files to be examined. When you specify archives, objdump shows information on each of the member object files.

The long and short forms of options, shown here as alternatives, are equivalent. At least one option besides `-l' must be given.

Page 23: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 23 Young Won Lim9/29/14

-g

-g Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.

Page 24: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 24 Young Won Lim9/29/14

-g

-v Verbose mode. Print out GNU CPP's version number at the beginning of execution, and report the final form of the include path.

Page 25: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 25 Young Won Lim9/29/14

GCC Options

gcc -W -Wall -O2 -o t t.c

-E-S-c-v--save-temps

Page 26: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 26 Young Won Lim9/29/14

CC1 Options

-ansi-std-traditional-fno -asm-W-Wall-O0-O1-O2-O3

-Os

Page 27: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 27 Young Won Lim9/29/14

Debug Options

-g-pg

Page 28: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 28 Young Won Lim9/29/14

Collect2, ld Options

-L-l-shared-static-nostdlib-nostartfiles-Wl-s-x-n-r-e-M-oformat

Page 29: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 29 Young Won Lim9/29/14

ELF

gcc -da t.c

as -V -Qy -o t.o t.s

Page 30: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 30 Young Won Lim9/29/14

Static Library

gcc -c t1.c t2.c

ar rscv libttt.a t1.o t2.o

gcc -o t t.c -l./ -lttt

ar rus [lib name] [obj files]

ar ds [lib name] [obj files]

ar x [lib name]

ar t [lib name]

Page 31: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 31 Young Won Lim9/29/14

Dynamic Shared Library

gcc -ipic -c t1.c t2.c

gcc -shared -wl,soname,libttt.so.0 -o libttt.so.0.0.0 t1.o t2.o

-ln -s libttt.so.0.0.0 libttt.so

-ln -s libttt.so.0.0.0 libttt.so.0

ldconfig

gcc -o t t.c -l./ -lttt

Page 32: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 32 Young Won Lim9/29/14

binutil

addr2line

ar

as

gprof

ld

nm

objcopy

objdump

ranlib

readelf

size

strip

Page 33: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 33 Young Won Lim9/29/14

addr2line

addr2line -fe t 0x80484a0

Page 34: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 34 Young Won Lim9/29/14

profile

gcc -g -o t t.c

gprof -a ./t

Page 35: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 35 Young Won Lim9/29/14

nm

ABCDGINRSTUVW-?

Page 36: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 36 Young Won Lim9/29/14

nm

-a-D-g-v-s-u

Page 37: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 37 Young Won Lim9/29/14

objcopy

objcopy t t.newobjcopy -o binary t t.newobjcopy -r,note -r,comment t t.newobjcopy -s t t.new

Page 38: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 38 Young Won Lim9/29/14

Objdump

Objdump -d t.oObjdump -S t.o

-a-x-t-T-r-R

Page 39: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Compiler 39 Young Won Lim9/29/14

strip

Strip R,note -R,comment -s t

Page 40: Compiler (1A) - Wikimedia · 9/26/2014  · Compiler 6 Young Won Lim 9/29/14 gcc 2.5 cpp, cpp0 cpp pre-processes a source file, such as including the contents of header files into

Young Won Lim9/29/14

References

[1] An Introduction to GCC, B. Gough, http://www.network-theory.co.uk/docs/gccintro/ [2] Unix, Linux Programming Indispensable Utilities, CW Paik