リンカ・ローダ実践開発テクニック

Similar documents

double float


program.dvi

‚æ4›ñ

超初心者用

untitled

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

第5回お試しアカウント付き並列プログラミング講習会

Condition DAQ condition condition 2 3 XML key value

untitled

ex01.dvi

slide5.pptx

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

r07.dvi

ohp07.dvi

1.ppt

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

joho07-1.ppt

ex01.dvi

2 1. Ubuntu 1.1 OS OS OS ( OS ) OS ( OS ) VMware Player VMware Player jp/download/player/ URL VMware Plaeyr VMware

Microsoft Word - C.....u.K...doc

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

C

O(N) ( ) log 2 N

J.JSSAC Vol. 7, No. 2, Mathematica Maple,., Open asir Open xxx asir. Open xxx Open asir, asir., Open xxx, Linux Open asir Open sm1 (kan/sm1). C

:30 12:00 I. I VI II. III. IV. a d V. VI

2008 IIA (program) pro(before)+gram(write) (artificial language) (programming languege) (programming) (machine language) (assembly language) ( )

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

memo

main main Makefile Makefile C.5 Makefile Makefile Makefile A Mech (TA ) 1. Web ( iku

XMPによる並列化実装2

:30 12:00 I. I VI II. III. IV. a d V. VI

ohp03.dvi

lexex.dvi

para02-2.dvi

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

新版明解C言語 実践編

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

slide4.pptx

スライド タイトルなし

(300, 150) 120 getchar() HgBox(x, y, w, h) (x, y), w, h #include <stdio.h> #include <handy.h> int main(void) { int i; double w, h; } HgO

yacc.dvi

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

I 2 tutimura/ I 2 p.1/??

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

r03.dvi

ex12.dvi

thesis.dvi

programmingII2019-v01

273? C

64bit SSE2 SSE2 FPU Visual C++ 64bit Inline Assembler 4 FPU SSE2 4.1 FPU Control Word FPU 16bit R R R IC RC(2) PC(2) R R PM UM OM ZM DM IM R: reserved


r08.dvi

SystemC 2.0を用いた簡易CPUバスモデルの設計

2008 DS T050049

DOPRI5.dvi

2017_08_ICN研究会_印刷用

WinHPC ppt

プログラミング及び演習 第1回 講義概容・実行制御

RHEA key

Microsoft PowerPoint - CproNt02.ppt [互換モード]

untitled

橡Pro PDF

Microsoft Word - Sample_CQS-Report_English_backslant.doc

,,,,., C Java,,.,,.,., ,,.,, i

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

2 T 1 N n T n α = T 1 nt n (1) α = 1 100% OpenMP MPI OpenMP OpenMP MPI (Message Passing Interface) MPI MPICH OpenMPI 1 OpenMP MPI MPI (trivial p

1 $ cat aboutipa 2 IPA is a Japanese quasi-government 3 organization established in accor- 4 dance with The Law for Information 5 Processing Technolog

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

2. OpenMP OpenMP OpenMP OpenMP #pragma#pragma omp #pragma omp parallel #pragma omp single #pragma omp master #pragma omp for #pragma omp critica

ohp08.dvi

新・明解C言語 ポインタ完全攻略

Taro-リストⅢ(公開版).jtd

Microsoft PowerPoint - CELF_DLLのRAM使用量削減_説明資料.ppt

大統一Debian勉強会 gdb+python拡張を使ったデバッグ手法

comment.dvi

導入基礎演習.ppt

RX600 & RX200シリーズ アプリケーションノート RX用仮想EEPROM

tuat1.dvi

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

v831&2_tp.PDF

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B


tcp/ip.key

(Version: 2017/4/18) Intel CPU 1 Intel CPU( AMD CPU) 64bit SIMD Inline Assemler Windows Visual C++ Linux gcc 2 FPU SSE2 Intel CPU do

解きながら学ぶC++入門編

分割コンパイル (2018 年度 ) 担当 : 笹倉 佐藤 分割コンパイルとは 一つのプログラムのソースを複数のソースファイルに分けてコンパイルすること ある程度大きなプログラムの場合ソースファイルをいくつかに分割して開発するのが普通 1

SystemC言語概論

untitled

新コンフィギュレータのフレームワークについて

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

Transcription:

11

12 main()

13.o.c.o hello.c main() hello.c printf() main.c lib1.c lib2.c main.o lib1.o lib2.o #include <stdio.h> int main() { printf("hello world! n"); exit (0); } -c main()

14 printf() exit() main() main() argc argv main() main()exit() exit() _exit() hello.c -v hello.c gcc -v /usr/libexec/elf/ld

15 hello.c gcc -v 001: % gcc hello.c -Wall -o hello -v 002: Using builtin specs. 003: gcc version 2.95.4 20020320 [FreeBSD] 004: /usr/libexec/cpp0 -lang-c -v -D GNUC =2 -D GNUC_MINOR =95 -Di386 -D FreeBSD =4 -D FreeBSD_cc_version=460001 -Dunix -D i386 -D FreeBSD =4 -D FreeBSD_cc_version =460001 -D unix -D i386 -D unix -Acpu(i386) -Amachine(i386) -Asystem(unix) -Asystem(FreeBSD) -Wall -Acpu(i386) -Amachine(i386) -Di386 -D i386 -D i386 -D ELF hello.c /tmp/ccqnujxp.i 005: GNU CPP version 2.95.4 20020320 [FreeBSD] (i386 FreeBSD/ELF) 006: #include "..." search starts here: 007: #include <...> search starts here: 008: /usr/include 009: /usr/include 010: End of search list. 011: The following default directories have been omitted from the search path: 012: /usr/include/g++ 013: End of omitted list. cc1 014: /usr/libexec/cc1 /tmp/ccqnujxp.i -quiet -dumpbase hello.c -Wall -version -o /tmp/ccbkxy21.s 015: GNU C version 2.95.4 20020320 [FreeBSD] (i386-unknown-freebsd) compiled by GNU C version 2.95.4 20020320 [FreeBSD]. 016: /usr/libexec/elf/as -v -o /tmp/ccs4wtkl.o /tmp/ccbkxy21.s 017: GNU assembler version 2.12.1 [FreeBSD] 2002-07-20 (i386-obrien-freebsd5.0) using BFD version 2.12.1 [FreeBSD] 2002-07-20 018: /usr/libexec/elf/ld -V -dynamic-linker /usr/libexec/ld-elf.so.1 -o hello /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/ccs4wtkl.o -lgcc -lc -lgcc /usr/lib/crtend.o /usr/lib/crtn.o 019: GNU ld version 2.12.1 [FreeBSD] 2002-07-20 020: Supported emulations: 021: elf_i386 022: % crt1.o crti.o crtbegin.o crtend.o crtn.o /tmp /tmp/ccs4wktl.o crt /tmp/ccswktl.o hello.c /usr/libexec/cpp0 /usr/libexec/elf/as /usr/libexec/elf/ld /tmp/ccswktl.o /tmp /usr/libexec/elf/as /usr/libexec/cc1

16 *.c cpp0 *.i cc1 *.s as *.o ld

17 *.o *.o size hello.c hello hello size hello dec hex const auto static static auto % gcc hello.c -o hello -Wall %./hello Hello world! % size hello text data bss dec hex filename 1042 208 28 1278 4fe hello %

18 exec() execve() objdump hello objdump objdump -h

19 % objdump -h hello hello: file format elf32-i386 Sections: Idx Name Size VMA LMA File off Algn 0.interp 00000019 080480f4 080480f4 000000f4 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 1.note.ABI-tag 00000018 08048110 08048110 00000110 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 2.hash 00000054 08048128 08048128 00000128 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 3.dynsym 00000100 0804817c 0804817c 0000017c 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 4.dynstr 0000009d 0804827c 0804827c 0000027c 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 5.rel.plt 00000018 0804831c 0804831c 0000031c 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 6.init 0000000b 08048334 08048334 00000334 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 7.plt 00000040 08048340 08048340 00000340 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 8.text 00000178 08048380 08048380 00000380 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 9.fini 00000006 080484f8 080484f8 000004f8 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 10.rodata 0000000f 080484fe 080484fe 000004fe 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 11.data 0000000c 08049510 08049510 00000510 2**2 CONTENTS, ALLOC, LOAD, DATA 12.eh_frame 00000004 0804951c 0804951c 0000051c 2**2 CONTENTS, ALLOC, LOAD, DATA 13.dynamic 00000098 08049520 08049520 00000520 2**2 CONTENTS, ALLOC, LOAD, DATA 14.ctors 00000008 080495b8 080495b8 000005b8 2**2 CONTENTS, ALLOC, LOAD, DATA 15.dtors 00000008 080495c0 080495c0 000005c0 2**2 CONTENTS, ALLOC, LOAD, DATA 16.got 00000018 080495c8 080495c8 000005c8 2**2 CONTENTS, ALLOC, LOAD, DATA 17.bss 0000001c 080495e0 080495e0 000005e0 2**2 ALLOC 18.comment 000000a0 00000000 00000000 000005e0 2**0 CONTENTS, READONLY 19.note 00000050 00000000 00000000 00000680 2**0 CONTENTS, READONLY % hello size which size size /usr/bin/size /usr/src/usr.bin /usr/src/usr.bin/size size.c size.c

20 001: int 002: show(count, name) 003: int count; 004: char *name; 005: { 006: static int first = 1; 007: struct exec head; 008: u_long total; 009: int fd; 010: 011: if ((fd = open(name, O_RDONLY, 0)) < 0) { 012: warn("%s", name); 013: return (1); 014: } 015: if (read(fd, &head, sizeof(head))!=sizeof(head) N_BADMAG(head)) { 016: (void)close(fd); 017: warnx("%s: not in a.out format", name); 018: return (1); 019: } 020: (void)close(fd); 021: 022: if (first) { 023: first = 0; 024: (void)printf("text tdata tbss tdec thex n"); 025: } 026: total = head.a_text + head.a_data + head.a_bss; 027: (void)printf("%lu t%lu t%lu t%lu t%lx", (u_long)head.a_text, 028: (u_long)head.a_data, (u_long)head.a_bss, total, total); 029: if (count > 1) 030: (void)printf(" t%s", name); 031: (void)printf(" n"); 032: return (0); 033: } struct exec struct exec /usr/include/sys/imgact_aout.h size hello struct exec 0x09010101 ELF

21 imgact_aout.h /* * Header prepended to each a.out file. * only manipulate the a_midmag field via the * N_SETMAGIC/N_GET{MAGIC,MID,FLAG} macros in a.out.h */ struct exec { unsigned long a_midmag; /* flags<<26 mid<<16 magic */ unsigned long a_text; /* text segment size */ unsigned long a_data; /* initialized data size */ unsigned long a_bss; /* uninitialized data size */ unsigned long a_syms; /* symbol table size */ unsigned long a_entry; /* entry point */ unsigned long a_trsize; /* text relocation size */ unsigned long a_drsize; /* data relocation size */ }; /* XXX Hack to work with current kern_execve.c */ #define a_magic a_midmag % hexdump -C hello head -n 3 00000000 7f 45 4c 46 01 01 01 09 00 00 00 00 00 00 00 00.ELF... 00000010 02 00 03 00 01 00 00 00 80 83 04 08 34 00 00 00...4... 00000020 7c 07 00 00 00 00 00 00 34 00 20 00 06 00 28 00...4....(. % size Makefile /usr/src/usr.bin/size/makefile BINDIR= /usr/libexec/aout size /usr/libexec/aout /usr/src/usr.bin/size size struct exec /usr/src/usr.bin/size size struct exec size size size size /usr/src/gnu/usr.bin/binutils/size /usr/src/contrib/ binutils/binutils/size.c /usr/libexec/elf

22 /usr/src/contrib/binutils/binutils/size.c berkeley_sum() ALLOC ALLOC CODE READONLY CONTENTS CONTENTS ALLOC LOAD CODE READONLY CODE READONLY CONTENTS ALLOC ALLOC size size size size /usr/src/contrib/binutils/binutils/size.c static bfd_size_type bsssize; static bfd_size_type datasize; static bfd_size_type textsize; static void berkeley_sum (abfd, sec, ignore) bfd *abfd ATTRIBUTE_UNUSED; sec_ptr sec; PTR ignore ATTRIBUTE_UNUSED; { flagword flags; bfd_size_type size; flags = bfd_get_section_flags (abfd, sec); if ((flags & SEC_ALLOC) == 0) return; size = bfd_get_section_size_before_reloc (sec); if ((flags & SEC_CODE)!= 0 (flags & SEC_READONLY)!= 0) textsize += size; else if ((flags & SEC_HAS_CONTENTS)!= 0) datasize += size; else bsssize += size; }

23 size objdump -p objdump --all-headers objdump % objdump -p hello hello: file format elf32-i386 Program Header: PHDR off 0x00000034 vaddr 0x08048034 paddr 0x08048034 align 2**2 filesz 0x000000c0 memsz 0x000000c0 flags r-x INTERP off 0x000000f4 vaddr 0x080480f4 paddr 0x080480f4 align 2**0 filesz 0x00000019 memsz 0x00000019 flags r-- LOAD off 0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12 filesz 0x0000050d memsz 0x0000050d flags r-x LOAD off 0x00000510 vaddr 0x08049510 paddr 0x08049510 align 2**12 filesz 0x000000d0 memsz 0x000000ec flags rw- DYNAMIC off 0x00000520 vaddr 0x08049520 paddr 0x08049520 align 2**2 filesz 0x00000098 memsz 0x00000098 flags rw- NOTE off 0x00000110 vaddr 0x08048110 paddr 0x08048110 align 2**2 filesz 0x00000018 memsz 0x00000018 flags r-- Dynamic Section: NEEDED libc.so.4 INIT 0x8048334 FINI 0x80484f8 HASH 0x8048128 STRTAB 0x804827c SYMTAB 0x804817c STRSZ 0x9d SYMENT 0x10 DEBUG 0x0 PLTGOT 0x80495c8 PLTRELSZ 0x18 PLTREL 0x11 JMPREL 0x804831c %

35

36 man elf jman /usr/include/elf.h elf.h

37 /usr/include/machine/elf.h /usr/include/sys/elf32.h /usr/include/sys/elf64.h /usr/include/sys/elf_common.h /usr/include/sys/elf_generic.h

38 elfsamp.c elfsamp.c main() main.c elfsamp.c elfsamp.c main.c elfsamp.o main.o elfsamp % gcc -c elfsamp.c -Wall % gcc -c main.c -Wall % gcc elfsamp.o main.o -Wall -o elfsamp elfsamp %./elfsamp sample % elfsamp.o elfsamp elfsamp.c main.c #include <stdio.h> static int s1, s2 = 1; int g1, g2 = 1; const int g3 = 1; extern int e3; void efunc(); static void sfunc() { efunc(); s1 = s2 = 10; g1 = g2 = 10; e3 = 10; } #include <stdio.h> int e3; void gfunc(); void efunc() {} int main() { gfunc(); exit (0); } void gfunc() { sfunc(); efunc(); g1 = e3 = 20; fprintf(stdout, "sample n"); }

39 elf32.h Elf32_Ehdr elf32.h Elf32_Ehdr elf64.h Elf64_Ehdr elf_genelic.h Elf32_Ehdr Elf_Ehdr Elf64_Ehdr Elf_Ehdr Elf32_Ehdr Elf64_Ehdr Elf_Ehdr elf_genelic.h Elf32_Ehdr e_ident[] EI_NIDENT /usr/include/sys/elf_common.h #define EI_NIDENT 16 /* Size of e_ident array. */ elf32.h /* * ELF header. */ typedef struct { unsigned char e_ident[ei_nident]; /* File identification. */ Elf32_Half e_type; /* File type. */ Elf32_Half e_machine; /* Machine architecture. */ Elf32_Word e_version; /* ELF format version. */ Elf32_Addr e_entry; /* Entry point. */ Elf32_Off e_phoff; /* Program header file offset. */ Elf32_Off e_shoff; /* Section header file offset. */ Elf32_Word e_flags; /* Architecture-specific flags. */ Elf32_Half e_ehsize; /* Size of ELF header in bytes. */ Elf32_Half e_phentsize; /* Size of program header entry. */ Elf32_Half e_phnum; /* Number of program header entries. */ Elf32_Half e_shentsize; /* Size of section header entry. */ Elf32_Half e_shnum; /* Number of section header entries. */ Elf32_Half e_shstrndx; /* Section name strings section. */ } Elf32_Ehdr;

40 e_ident[] e_ident[] sys/elf_common.h Elf_Ehdr *ehdr; if ((ehdr->e_ident[ei_class] == ELFCLASS32) &&(ehdr->e_ident[ei_data] == ELFDATA2LSB)) ELFCLASS32 ELFDATA2LSB elf_generic.h ELF_ CLASS ELF_DATA Elf_Ehdr *ehdr; if ((ehdr->e_ident[ei_class] == ELF_CLASS) &&(ehdr->e_ident[ei_data] == ELF_DATA)) Elf_Ehdr 0x7f 'E' 'L' 'F' ELFCLASSNONE ELFCLASS32 ELFCLASS64 ELFDATANONE ELFDATA2LSB ELFDATA2MSB EV_NONE EV_CURRENT ELFOSABI_NETBSD ELFOSABI_LINUX ELFOSABI_SOLARIS ELFOSABI_FREEBSD... 0

41 e_ident[] elf_common.h man elf elf_common.h readelf elfsamp.o readelf -h ELF Header Magic e_ident[] Class Data Version OS/ABI ABI Version e_ident[] ABI Version Type Machine Version e_type e_machine e_version e_type e_machine e_version e_entry e_phoff e_shoff e_flags e_ehsize e_phentsize e_phnum e_shentsize e_shnum e_shstrndx ET_EXEC ET_REL ET_CORE ET_DYN EM_386 EV_CURRENT _start() sizeof(elf_ehdr) sizeof(elf_phdr) e_phentsize e_phnum sizeof(elf_shdr) e_shentsize e_shnum e_shstrndx