Skip to content

Commit e8b906c

Browse files
committed
review
1 parent 49c3c47 commit e8b906c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3548
-0
lines changed

chapter7/README

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
CODE:
3+
P449 - main.c swap.c
4+
P455 - foo3.c bar3.c
5+
P458 - addvec.c multvec.c main2.c
6+
7+
8+
Blogs:
9+
1.探寻ELF文件内部:
10+
http://blog.csdn.net/vonzhoufz/article/details/44925439
11+
12+
2.创建并使用静态库(ar 命令)
13+
http://blog.csdn.net/vonzhoufz/article/details/44463977
14+

chapter7/README~

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
CODE:
3+
P449 - main.c swap.c
4+
P455 - foo3.c bar3.c
5+
P458 - addvec.c multvec.c main2.c
6+
7+
8+
9+
1.探寻ELF文件内部:
10+
http://blog.csdn.net/vonzhoufz/article/details/44925439
11+
12+
2.创建并使用静态库(ar 命令)
13+
http://blog.csdn.net/vonzhoufz/article/details/44463977
14+

chapter7/addvec.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* addvec.c */
2+
void addvec(int *x, int *y,
3+
int *z, int n)
4+
{
5+
int i;
6+
7+
for (i = 0; i < n; i++)
8+
z[i] = x[i] + y[i];
9+
}
10+

chapter7/bar3.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
int x ;//weak;
3+
4+
int f(){
5+
x = 15212;
6+
}

chapter7/bar4.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
int x ;//weak;
3+
4+
int f(){
5+
x = 15212;
6+
}

chapter7/bar5.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
double x ;//weak;
3+
4+
int f(){
5+
x = -0.0;
6+
}

chapter7/dll.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <dlfcn.h>
4+
5+
int x[2] = {1, 2};
6+
int y[2] = {3, 4};
7+
int z[2];
8+
9+
int main()
10+
{
11+
void *handle;
12+
void (*addvec)(int *, int *, int *, int);
13+
char *error;
14+
15+
/* dynamically load the shared library that contains addvec() */
16+
handle = dlopen("./libvector.so", RTLD_LAZY);
17+
if (!handle) {
18+
fprintf(stderr, "%s\n", dlerror());
19+
exit(1);
20+
}
21+
22+
/* get a pointer to the addvec() function we just loaded */
23+
addvec = dlsym(handle, "addvec");
24+
if ((error = dlerror()) != NULL) {
25+
fprintf(stderr, "%s\n", error);
26+
exit(1);
27+
}
28+
29+
/* Now we can call addvec() it just like any other function */
30+
addvec(x, y, z, 2);
31+
printf("z = [%d %d]\n", z[0], z[1]);
32+
33+
/* unload the shared library */
34+
if (dlclose(handle) < 0) {
35+
fprintf(stderr, "%s\n", dlerror());
36+
exit(1);
37+
}
38+
return 0;
39+
}
40+

chapter7/elfstructs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 (4 bits) */
6+
binding:4; /* local or global (4 bits) */
7+
char reserved; /* unused */
8+
char section; /* section header index, ABS, UNDEF, */
9+
/* or COMMON */
10+
} Elf_Symbol;
11+
12+
typedef struct {
13+
int offset; /* offset of the reference to relocate */
14+
int symbol:24, /* symbol the reference should point to */
15+
type:8; /* relocation type */
16+
} Elf32_Rel;
17+

chapter7/foo3.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
void f();
4+
5+
int x = 15213; // strong
6+
7+
int main(){
8+
f();
9+
printf("x=%d\n", x);
10+
return 0;
11+
}

chapter7/foo4.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
void f();
4+
5+
int x;
6+
int main(){
7+
x = 15213;
8+
f();
9+
printf("x=%d\n", x);
10+
return 0;
11+
}

0 commit comments

Comments
 (0)