Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7.

Similar presentations


Presentation on theme: "1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7."— Presentation transcript:

1 1 Linking

2 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7

3 3 main.cswap.c 1./*main.c */ 2.void swap() ; 3. 4.int buf[2] = {1, 2}; 5. 6.Int main() 7.{ 8. swap() ; 9. return 0 ; 10.} 1./*swap.c */ 2.extern int buf[]; 3. 4.int *bufp0 = &buf[0] 5.int *bufp1 ; 6. 7.void swap() 8.{ 9. int temp ; 10. 11. bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

4 4 ELF Symbol Tables 1. typedef struct { 2. int name ;/* string table offset */ 3. int value ;/* section offset, or VM address */ 4. int size ;/* object size in bytes */ 5. char type:4, /* data, func, section, or src file name */ 6. binding:4 ;/* local or global */ 7. char reserved ;/* unused */ 8. char section ;/* section header index, ABS, UNDEF, */ 9. /* or COMMON */ 10. } ABS, UNDEF, COMMON

5 5 Num:ValueSizeType BindOtNdxName 8: 0 8 OBJECT GLOBAL03buf 9: 0 17 FUNC GLOBAL01main 10: 0 0 NOTYPE GLOBAL0UNDswap Num:ValueSizeType BindOtNdxName 8: 0 4 OBJECT GLOBAL03bufp0 9: 0 0 NOTYPE GLOBAL0UNDbuf 10: 0 39 FUNC GLOBAL01swap 11: 4 4 OBJECT GLOBAL0COMbufp1 alignment ELF Symbol Tables

6 6 (a) C code 1 extern int p3(void); 2 int x = 1; 3 int *xp = &x; 4 5 void p2(int y) { 6 } 7 8 void p1() { 9 p2(*xp + p3()); 10 } Example (Relocation)

7 7 (b).text section of relocatable object file 1 00000000 : 2 0: 55 push %ebp 3 1: 89 e5 mov %esp,%ebp 4 3: 89 ec mov %ebp,%esp 5 5: 5d pop %ebp 6 6: c3 ret Example (Relocation)

8 8 7 00000008 : 8 8: 55 push %ebp 9 9: 89 e5 mov %esp,%ebp 10 b: 83 ec 08 sub $0x8,%esp 11 e: 83 c4 f4 add $0xfffffff4,%esp 12 11: e8 fc ff ff ff call 12 13 16: 89 c2 mov %eax,%edx 14 18: a1 00 00 00 00 mov 0x0,%eax 15 1d: 03 10 add (%eax),%edx 16 1f: 52 push %edx 17 20: e8 fc ff ff ff call 21 18 25: 89 ec mov %ebp,%esp 19 27: 5d pop %ebp 20 28: c3 ret Example (Relocation)

9 9 (c).data section of relocatable object file 1 00000000 : 2 0: 01 00 00 00 3 00000004 : 4 4: 00 00 00 00 Example (Relocation)

10 10 Relocation –Merge the input modules –Assign runtime address to each symbol Two steps –Relocating sections and symbol definitions –Relocating symbol references within sections

11 11 Relocation For each reference to an object with unknown location –Assembler generates a relocation entry –Relocation entries for code are placed in.rel.text –Relocation entries for data are placed in.rel.data

12 12 Relocation Relocation Entry typedef struct { int offset ; int symbol:24, type:8 ; } Elf32_Rel ;

13 13 Relocation 6e8 fc ff ff ffcall 7 swap(); There is a relocation entry in rel.txt offsetsymboltype 7swapR_386_PC32

14 14 Relocation int *bufp0 = &buf[0] ; 00000000 : 0: 00 00 00 00 There is a relocation entry in rel.data offsetsymboltype 0bufR_386_32

15 15 6e8 fc ff ff ffcall 7 swap(); 7: R_386_PC32 swap relocation entry r.offest = 0x7 r.symbol = swap r.type = R_386_PC32 ADDR(main)=ADDR(.text) = 0x80483b4 ADDR(swap)=0x80483c8 refaddr = ADDR(main)+r.offset = 0x80483bb ADDR(r.symbol)=ADDR(swap)=0x80483c8 *refptr = (unsigned) (ADDR(r.symbol) + *refptr – refaddr = (unsigned) (0x80483c8 + (-4) – 0x80483bb) = (unsigned) 0x9 Relocation

16 16 int *bufp0 = &buf[0] ; 00000000 : 0: 00 00 00 00int *bufp0 = &buf[0]; 0: R_386_32 bufrelocation entry ADDR(r.symbol) = ADDR(buf) = 0x8049454 *refptr = (unsigned) (ADDR(r.symbol)+ *refptr) = (unsigned) (0x8049454) 0804945c : 0804945c:54 94 04 08 Relocation

17 17 1foreach section s { 2 foreach relocation entry r { 3 refptr = s + r.offset ; /* ptr to reference to be relocated */ 4 5 /* relocate a PC-relative reference */ 6 if (r.type == R_386_PC32) { 7 refaddr = ADDR(s) + r.offset ; /* ref’s runtime address */ 8 *refptr=(unsigned) (ADDR(r.symbol)+*refptr –refaddr) ; 9 } 10 11 /* relocate an absolute reference */ 12 if ( r.type == R_386_32 ) 13 *refptr = (unsigned) (ADDR(r.symbol) + *refptr) ; 14 } 15 } Relocation

18 18 1080483b4 : 2080483b4: 55push %ebp 3080483b5: 89 e5mov %esp, %ebp 4080483b7: 83 ec 08sub $0x8, %esp 5080483ba: e8 09 00 00 00call 80483c8 6080483bf: 31 c0xor %eax, %eax 7080483c1: 89 ecmov %ebp, %esp 8080483c3: 5dpop %ebp 9080483c4: c3ret 10080483c5: 90nop 11080483c6: 90nop 12080483c7: 90nop Relocation

19 19 13080483c8 : 14 80483c8:55 push%ebp 15 80483c9: 8b 15 5c 94 04 08 mov0x804945c, %edx get *bufp0 16 80483cf: a1 58 94 04 08 mov0x8049458, %edx get buf[1] 17 80483d4:89 e5 mov%esp, %ebp 18 80483d6:c7 05 48 95 04 08 movl $0x8049458, 0x8049548 19 80483de: 58 94 04 08 bufp1 = &buf[1] 20 80483e0:89 ec mov%ebp, %esp 21 80483e2:8b 0a mov (%edx), %ecx 22 80483e4:80 02 mov%eax, (%edx) 23 80483e6:a1 48 95 04 08 mov0x8049548, %eax 24 80483eb:89 08 mov%ecx, (%eax) 25 80483ed:5d pop%ebp 26 80483ee:c3 ret Relocation

20 20 1 08049454 : 2 8049454:01 00 00 00 02 00 00 00 3 0804945c : 4 804945c:54 94 04 08 Relocation

21 21 1 00000000 : 2 0: 55 push %ebp 3 1: 8b 15 00 00 00 00 mov 0x0,%edx Get *bufp0=&buf[0] 4 3: R_386_32 bufp0 Relocation entry 5 7: a1 04 00 00 00 mov 0x4,%eax Get buf[1] 6 8: R_386_32 buf Relocation entry 7 c: 89 e5 mov %esp,%ebp 8 e: c7 05 00 00 00 00 movl $0x4,0x0 bufp1 = &buf[1]; 9 14: 04 00 00 00 10 10: R_386_32 bufp1 Relocation entry 11 14: R_386_32 buf Relocation entry Example (Relocation)

22 22 12 18: 89 ec mov %ebp,%esp 13 1a: 8b 0a mov (%edx),%ecx temp = buf[0]; 14 1c: 89 02 mov %eax,(%edx) buf[0]=buf[1]; 15 1e: a1 00 00 00 00 mov 0x0,%eax Get *bufp1=&buf[1] 16 1f: R_386_32 bufp1 Relocation entry 17 23: 89 08 mov %ecx,(%eax) buf[1]=temp; 18 25: 5d pop %ebp 19 26: c3 ret ADDR(swap)=0x80483c8 ADDR(buf) = 0x8049454 ADDR(bufp0)=0x804945c ADDR(bufp1)=0x8049548 Example (Relocation)

23 23 Example (Relocation) Line # in Fig. 7.10AddressValue

24 24 (a) C code 1 int relo3(int val) { 2 switch (val) { 3 case 100: 4 return(val); 5 case 101: 6 return(val+1); 7 case 103: case 104: 8 return(val+3); 9 case 105: 10 return(val+5); 11 default: 12 return(val+6); 13 } 14 } Example (Relocation)

25 25 (b).text section of relocatable object file 1 00000000 : 2 0: 55 push %ebp 3 1: 89 e5 mov %esp,%ebp 4 3: 8b 45 08 mov 0x8(%ebp),%eax 5 6: 8d 50 9c lea 0xffffff9c(%eax),%edx 6 9: 83 fa 05 cmp $0x5,%edx 7 c: 77 17 ja 25 8 e: ff 24 95 00 00 00 00 jmp *0x0(,%edx,4) Example (Relocation)

26 26 9 15: 40 inc %eax 10 16: eb 10 jmp 28 11 18: 83 c0 03 add $0x3,%eax 12 1b: eb 0b jmp 28 13 1d: 8d 76 00 lea 0x0(%esi),%esi 14 20: 83 c0 05 add $0x5,%eax 15 23: eb 03 jmp 28 16 25: 83 c0 06 add $0x6,%eax 17 28: 89 ec mov %ebp,%esp 18 2a: 5d pop %ebp 19 2b: c3 ret Example (Relocation)

27 27 (c).rodata section of relocatable object file This is the jump table for the switch statement 10000 28000000 15000000 25000000 18000000 20010 18000000 20000000 Example (Relocation)


Download ppt "1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7."

Similar presentations


Ads by Google