link 6. static linking · link6. staticlinking youngw.lim 2018-10-04thr youngw.lim link6....

24
Link 6. Static Linking Young W. Lim 2018-10-04 Thr Young W. Lim Link 6. Static Linking 2018-10-04 Thr 1 / 24

Upload: others

Post on 17-Jul-2020

5 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Link 6. Static Linking

Young W. Lim

2018-10-04 Thr

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 1 / 24

Page 2: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Outline

1 Linking - 6. Static LinkingBased onStatic Library ExamplesLinking with Static LibrariesResolving refereces with Static Libraries

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 2 / 24

Page 3: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Based on

"Self-service Linux: Mastering the Art of Problem Determination",Mark Wilding"Computer Architecture: A Programmer’s Perspective",Bryant & O’Hallaron

I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted tocopy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2or 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.

CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short:you are free to share and make derivative works of the file under the conditions that you appropriately attribute it,and that you distribute it only under a license compatible with this one.

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 3 / 24

Page 4: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

addvec.c and multvec.c

/*::::: addvec.c :::::::::::::::::::::::::*/void addvec(int *x, int *y, int *z, int n){

int i;

for (i=0; i<n; i++)z[i] = x[i] + y[i];

}

/*::::: multvec.c :::::::::::::::::::::::::*/void multvec(int *x, int *y, int *z, int n){

int i;

for (i=0; i<n; i++)z[i] = x[i] * y[i];

}

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 4 / 24

Page 5: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

libvector.a

gcc -c addvec.caddvec.o

gcc -c multvec.cmultvec.o

ar rcs libvector.a addvec.o multvec.olibvector.a

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 5 / 24

Page 6: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

main.c

/*::::: vector.h ::::::::::::::::::::::::::*/void addvec(int *x, int *y, int *z, int n);void multvec(int *x, int *y, int *z, int n);

/*::::: main.c ::::::::::::::::::::::::::::*/#include <stdio.h>#include "vector.h"

int x[2] = { 1, 2};int y[2] = { 3, 4};int z[2];

int main() {

addvec(x, y, z, 2);printf("z= [%d %d]\n", z[0], z[1]);

}

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 6 / 24

Page 7: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

p

gcc -O2 -c main.cmain.o

gcc -static -o p main.o ./libvector.ap

./pz= [4 6]

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 7 / 24

Page 8: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Static Libraries

assumption

the linker reads a collecitonof relocatable object filesand links them together into an output file

in practice

a static library is a mechanism for packagingthose related object modules into a single filethis file is supplied as input to the linkerthe linker copies only thoseobject modules in the librarythat are referenced by the application program

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 8 / 24

Page 9: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

ANSI C libc.a

libc.a libraryan extensive collection of standard I/O

atoi, pritnf, scanfstring manipulation

strcpyinteger math functions

random

libm.a libraryan extensive collection of floating-point math functions

sin, cos, sqrt

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 9 / 24

Page 10: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

advantages of static libraries (1)

related functions can be compiledinto separate object modulesthen packaged in a single static library fileapplication program can then useany of the functions defined in the libraryby specifying the file name on the command linegcc main.c /usr/lib/libc.a /usr/lib/libm.a

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 10 / 24

Page 11: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

advantages of static libraries (2)

at link time, the linker willonly copy the object modulesthat are referenced by the programwhich reduces the size of the executableon disk and in memorythe application programmers only needto include the names of a few library filesC compiler drivers always pass libc.a to the linkerso the reference to libc.a is unnecessary

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 11 / 24

Page 12: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Unix archive

an archive on Unix systems

static libraries are stored on diskin a particular file format : archivea collection of concatenated relocatable object filesa header describes the size and locatonof each member object filearchive filenames are denoted with the .a suffix

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 12 / 24

Page 13: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Linking with static library examples (1)

main2.c ⇒ main2.otranslators (cpp, ccl, as)

libvector.a → addvec.o

libc.a → printf.oany other modules called by printf.o also

main2.o, addvec.o, printf.o, other object files ⇒ p2linker (ld)

void addvec(int *x, int *y)

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 13 / 24

Page 14: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Linking with static library examples (2)

source files : main2.c, vector.hstatic libraries : libvector.a, libc.arelocatable object files : main2.o, addvec.o, printf.o,any other modules called by printf.o

fully linked executable object file : p2

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 14 / 24

Page 15: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Symbol resolution phase

during the symbol resolution phase, the linker scansthe relocatable object files and archivesleft to right (⇒) in the same orderthat they appear on the command linethe linker maintains

a set E of relocatable object filesthat will be merged to form the executablea set U of unresolved symbolssymbols referred to but not yet defineda set D of symbols that have been definedin the previous input filesinitially, all the set E , U, D are empty

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 15 / 24

Page 16: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

for each input file f

for each input file f on the command line,the linker determines if f is an object fileor an archivefor input file f , the linker

adds f to Eupdates U and Dto reflect the symbol definitions and references in fand proceeds to the next input file

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 16 / 24

Page 17: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

for each input archive f

the linker attempts to matchthe unresolved symbols in Uagainst the symbols definedby the members of the archiveany member object files which arenot contained in E are discardedand the linker proceeds to the next input file

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 17 / 24

Page 18: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

for each member in the archive f

if some archive member m defines a symbolthat resolves a reference in Uthen m is added to E

then the linker updates U and Dto reflect the symbol definitions and references in m

this process iterates overthe member of object files in funtil a fixed point is reachedwhere U and D no longer change

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 18 / 24

Page 19: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

for undefined symbol set U is not empty

if U is nonempty when the linker finishesscanning the input files on the command line,it prints an error and terminatesotherwise, it merges and relocatesthe object files in Eto build the output executable file

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 19 / 24

Page 20: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Link time error

the ordering of libraries and object fileson the command line is significantif the library that defines a symbolappear on the command linebefore the object file that references the symbolthen the reference will not be resolvedand the linking will fail

void addvec(int *x, int *y)

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 20 / 24

Page 21: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Linke time error example

when libvector.a is processed, U is emptytherefore, no member object files from libvector.ais added to E

the reference to addvec is never resolvederror messagegcc -static ./libvector.a main2.c

in function ’main’ :undefined reference to ’addvec’

gcc -static main2.c ./libvector.a

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 21 / 24

Page 22: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Ordering libraries on the command line

if the members of different libraries are independent(no member references a symbol defined by other member)then the libraries can be placedat the end of the command line in arbitrary orderif they not independent, they must be orderedso that for each symbol s that is referencedexternally by a member of an archive,at least one definition of sfollows a reference to s

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 22 / 24

Page 23: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Ordering libraries on the command line example (1)

foo.c calls functions in libx.a and libz.awhich call functions in liby.a

libx.a and libz.a must precedes liby.aon the command linegcc foo.c libx.a libz.a liby.a

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 23 / 24

Page 24: Link 6. Static Linking · Link6. StaticLinking YoungW.Lim 2018-10-04Thr YoungW.Lim Link6. StaticLinking 2018-10-04Thr 1/24

Ordering libraries on the command line example (2)

libraries can be repeated on the command lineif necessary to satisfy the dependence requirementsfoo.c calls a function in libx.awhich calls a function in liby.awhich again calls a function in libx.a

then libx.a must be repeated on the command linegcc foo.c libx.a liby.a libx.a

alternatively, libx.a and liby.a can be combinedinto a single archive

Young W. Lim Link 6. Static Linking 2018-10-04 Thr 24 / 24