├── README.md ├── chapter11 └── ctors_section.c ├── chapter3 ├── simple_section.c └── special_symbol.c ├── chapter4 ├── a.c ├── b.c ├── hello.c └── hello.lds ├── chapter6 ├── heap_size.c └── section_mapping.c ├── chapter7 ├── auxv.c ├── lib.c ├── lib.h ├── program1.c └── program2.c ├── chapter8 ├── lib.c ├── lib.h ├── lib.ver ├── lib_old.ver └── program1.c └── chapter9 ├── LoadLibrary.c ├── Math.c └── TestMath.c /README.md: -------------------------------------------------------------------------------- 1 | link-load-library-code 2 | ====================== 3 | 4 | <程序员的自我修养> 源代码 5 | -------------------------------------------------------------------------------- /chapter11/ctors_section.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void my_init(void) 4 | { 5 | printf("hello from my_init\n"); 6 | } 7 | 8 | typedef void (*ctor_func)(void); 9 | ctor_func __attribute__((section (".ctors"))) my_init_p = &my_init; 10 | 11 | int main() 12 | { 13 | printf("hello from main...\n"); 14 | printf("%p\n", &my_init_p); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chapter3/simple_section.c: -------------------------------------------------------------------------------- 1 | /* 2 | * simple_section.c 3 | * 4 | * gcc -c simple_section.c 5 | */ 6 | 7 | int printf(const char* format, ...); 8 | 9 | int global_init_var = 84; 10 | int global_uninit_var; 11 | static int global_static_var; 12 | 13 | extern int reference_to_out; 14 | 15 | void func1(int i) { 16 | printf("%d\n", i); 17 | } 18 | 19 | int main(void) { 20 | static int static_var = 85; 21 | static int static_var2; 22 | 23 | int a = 1; 24 | int b; 25 | 26 | func1(static_var + static_var2 + a + b); 27 | 28 | return a; 29 | } 30 | -------------------------------------------------------------------------------- /chapter3/special_symbol.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ld will define some special define when linking 3 | */ 4 | 5 | #include 6 | 7 | extern char __executable_start[]; 8 | extern char etext[], _etext[], __etext[]; 9 | extern char edata[], _edata[]; 10 | extern char end[], _end[]; 11 | 12 | int main() { 13 | printf("Executable Start %p\n", __executable_start); 14 | printf("Text End %p %p %p\n", etext, _etext, __etext); 15 | printf("Data End %p %p\n", edata, _edata); 16 | printf("Executable End %p %p\n", end, _end); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chapter4/a.c: -------------------------------------------------------------------------------- 1 | extern int shared; 2 | 3 | int main() 4 | { 5 | int a = 0; 6 | swap(&a, &shared); 7 | } 8 | -------------------------------------------------------------------------------- /chapter4/b.c: -------------------------------------------------------------------------------- 1 | int shared = 1; 2 | int test = 3; 3 | 4 | void swap(int* a, int* b) { 5 | *a ^= *b ^= *a ^= *b; 6 | } 7 | -------------------------------------------------------------------------------- /chapter4/hello.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Tiny hello world. 3 | * gcc -c -fno-builtin hello.c 4 | * ld -static -T hello.lds -o hello hello.o 5 | */ 6 | 7 | char* str = "Hello world!\n"; 8 | 9 | void print() { 10 | asm("movl $13, %%edx\n\t" 11 | "movq %0, %%rcx\n\t" 12 | "movl $0, %%ebx\n\t" 13 | "movl $4, %%eax\n\t" 14 | "int $0x80 \n\t" 15 | ::"r"(str):"edx","ecx","ebx"); 16 | } 17 | 18 | void exit() { 19 | asm("movl $42,%ebx\n\t" 20 | "movl $1, %eax\n\t" 21 | "int $0x80 \n\t"); 22 | } 23 | 24 | void nomain() { 25 | print(); 26 | exit(); 27 | } 28 | -------------------------------------------------------------------------------- /chapter4/hello.lds: -------------------------------------------------------------------------------- 1 | ENTRY(nomain) 2 | 3 | SECTIONS 4 | { 5 | . = 0x08048000 + SIZEOF_HEADERS; 6 | tinytext : { *(.text) *(.data) *(.rodata) } 7 | /DISCARD/ : { *(.comment) } 8 | } 9 | -------------------------------------------------------------------------------- /chapter6/heap_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | size_t maximum = 0; 5 | 6 | int main(int argc, char* argv[]) { 7 | 8 | unsigned blocksize[] = { 1024 * 1024 * 1024, 1024 * 1024, 1024, 1 }; 9 | int i, count; 10 | 11 | for (i = 0; i < 3; i++) { 12 | for (count = 1;; count++) { 13 | void* block = malloc(maximum + blocksize[i] * count); 14 | if (block) { 15 | maximum = maximum + blocksize[i] * count; 16 | free(block); 17 | } else { 18 | break; 19 | } 20 | } 21 | } 22 | 23 | printf("maximum malloc size = %lu bytes\n", maximum); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chapter6/section_mapping.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gcc -static section_mapping.c -o section_maping.elf 3 | */ 4 | 5 | #include 6 | 7 | int main() { 8 | while (1) { 9 | sleep(1000); 10 | } 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chapter7/auxv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | void** p = (void**)argv; 7 | printf("%p\n", p); 8 | 9 | printf("Argument count: %d\n", *((int*)p - 1)); 10 | 11 | int i; 12 | for (i = 0; i < argc; ++i) 13 | { 14 | printf("Argument %d: %s\n", i, (char*)*p); 15 | p++; 16 | } 17 | 18 | // skip 0 19 | p++; 20 | 21 | printf("Environment:\n"); 22 | while (*p) { 23 | printf("%s\n", (char*)*p); 24 | p++; 25 | } 26 | 27 | // skip 0 28 | p++; 29 | 30 | printf("Auxiliary Vectors:\n"); 31 | Elf64_auxv_t* aux = (Elf64_auxv_t*)p; 32 | while (aux->a_type != AT_NULL) { 33 | printf("Type: %02ld Value: %#lx\n", aux->a_type, aux->a_un.a_val); 34 | aux++; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /chapter7/lib.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | #include 3 | 4 | void foobar(int i) { 5 | printf("Printing from lib.so --> %d\n", i); 6 | sleep(-1); 7 | } 8 | -------------------------------------------------------------------------------- /chapter7/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H_ 2 | #define LIB_H_ 3 | 4 | void foobar(int i); 5 | 6 | #endif // LIB_H_ 7 | -------------------------------------------------------------------------------- /chapter7/program1.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int main() { 4 | foobar(1); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /chapter7/program2.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | #include 3 | 4 | int main() { 5 | foobar(2); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /chapter8/lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gcc -shared -fPIC lib.c -Xlinker --version-script lib.ver -o lib.so 3 | */ 4 | 5 | #include "lib.h" 6 | #include 7 | 8 | void foobar(int i) { 9 | printf("Printing from lib.so --> %d\n", i); 10 | sleep(-1); 11 | } 12 | -------------------------------------------------------------------------------- /chapter8/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H_ 2 | #define LIB_H_ 3 | 4 | void foobar(int i); 5 | 6 | #endif // LIB_H_ 7 | -------------------------------------------------------------------------------- /chapter8/lib.ver: -------------------------------------------------------------------------------- 1 | VERS_1.2 { 2 | global: 3 | foobar; 4 | local: 5 | *; 6 | }; 7 | -------------------------------------------------------------------------------- /chapter8/lib_old.ver: -------------------------------------------------------------------------------- 1 | VERS_1.1 { 2 | global: 3 | foobar; 4 | local: 5 | *; 6 | }; 7 | -------------------------------------------------------------------------------- /chapter8/program1.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int main() { 4 | foobar(1); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /chapter9/LoadLibrary.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cl /c LoadLibrary.c 3 | * link LoadLibrary.objb 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | typedef double (*Func)(double, double); 10 | 11 | int main(int argc, char* argv[]) 12 | { 13 | Func function; 14 | double result; 15 | 16 | // Load DLL 17 | HINSTANCE hinstLib = LoadLibrary("Math.dll"); 18 | if (hinstLib == NULL) { 19 | printf("ERROR: unable to load DLL\n"); 20 | return 1; 21 | } 22 | 23 | // Get function address 24 | function = (Func)GetProcAddress(hinstLib, "Add"); 25 | if (function == NULL) { 26 | printf("ERROR: unable to find DLL function\n"); 27 | FreeLibrary(hinstLib); 28 | return 1; 29 | } 30 | 31 | // Call function. 32 | result = function(1.0, 2.0); 33 | 34 | // Unload DLL file 35 | FreeLibrary(hinstLib); 36 | 37 | // Display result 38 | printf("Result = %f\n", result); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /chapter9/Math.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cl /LDd Math.c 3 | */ 4 | 5 | __declspec(dllexport) double Add(double a, double b) 6 | { 7 | return a + b; 8 | } 9 | 10 | __declspec(dllexport) double Sub(double a, double b) 11 | { 12 | return a - b; 13 | } 14 | 15 | __declspec(dllexport) double Mul(double a, double b) 16 | { 17 | return a * b; 18 | } 19 | -------------------------------------------------------------------------------- /chapter9/TestMath.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cl /c TestMath.c 3 | * link TestMath.obj Math.lib 4 | */ 5 | 6 | #include 7 | 8 | __declspec(dllimport) double Sub(double a, double b); 9 | 10 | int main(int argc, char* argv[]) 11 | { 12 | double result = Sub(3.0, 2.0); 13 | printf("Result = %f\n", result); 14 | return 0; 15 | } --------------------------------------------------------------------------------