Ch7 ELF 실습
실습 설명을 읽고 소스를 확인한다. 원본 파일 링크는 소스 텍스트를 연다.
archive-cycle/main.c
#include <stdio.h>
extern int x(int value);
int main(void)
{
printf("x(3) = %d\n", x(3));
return 0;
}
archive-cycle/y.c
extern int x_helper(int value);
int y(int value)
{
return x_helper(value) + 10;
}
aslr-pie/addresses.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int stack_value = 0;
int *heap_value = malloc(sizeof(*heap_value));
if (heap_value == NULL) {
return 1;
}
printf("main=%p stack=%p heap=%p\n",
(void *)&main, (void *)&stack_value, (void *)heap_value);
free(heap_value);
return 0;
}
common-common/main.c
#include <stdio.h>
int x;
void update(void);
int main(void)
{
x = 15213;
update();
printf("x = %d\n", x);
return 0;
}
common-mismatch/main.c
#include <stdio.h>
int y = 0x3b6c;
int x = 0x3b6d;
void overwrite(void);
int main(void)
{
overwrite();
printf("x = 0x%x y = 0x%x\n", x, y);
return 0;
}
common-size/main.c
#include <stdio.h>
extern char arena[];
int main(void)
{
arena[31] = 42;
printf("arena[31]=%d\n", arena[31]);
return 0;
}
compiler-driver/main.c
#include <stdio.h>
extern int foo(int value);
int main(void)
{
int input;
if (scanf("%d", &input) != 1) {
return 1;
}
printf("output is %d\n", foo(input));
return 0;
}
duplicate-function/main.c
int helper(void)
{
return 1;
}
int main(void)
{
return helper() != 1;
}
explicit-weak/main.c
#include <stdio.h>
int hook = 7;
int provider_value(void);
int main(void)
{
printf("hook=%d provider-sees=%d\n", hook, provider_value());
return 0;
}
explicit-weak/provider.c
int hook __attribute__((weak)) = 3;
int provider_value(void)
{
return hook;
}
lto-dead-code/main.c
#include <stdio.h>
int cube(int x);
int main(void)
{
printf("cube(3) = %d\n", cube(3));
return 0;
}
lto-dead-code/math.c
int cube(int x)
{
return x * x * x;
}
int unused_helper(int x)
{
return x + 1000;
}
static-internal/main.c
#include <stdio.h>
static int x = 11;
int other_value(void);
int main(void)
{
printf("main.x=%d other.x=%d\n", x, other_value());
return 0;
}
static-library/addvec.c
#include "vector.h"
int addvec_calls;
void addvec(const int *left, const int *right, int *result, int length)
{
int index;
++addvec_calls;
for (index = 0; index < length; ++index) {
result[index] = left[index] + right[index];
}
}
static-library/main.c
#include <stdio.h>
#include "vector.h"
int main(void)
{
int left[2] = {1, 2};
int right[2] = {3, 4};
int result[2];
addvec(left, right, result, 2);
printf("result = [%d, %d]\n", result[0], result[1]);
return 0;
}
static-library/multvec.c
#include "vector.h"
int multvec_calls;
void multvec(const int *left, const int *right, int *result, int length)
{
int index;
++multvec_calls;
for (index = 0; index < length; ++index) {
result[index] = left[index] * right[index];
}
}
static-library/vector.h
#ifndef VECTOR_H
#define VECTOR_H
void addvec(const int *left, const int *right, int *result, int length);
void multvec(const int *left, const int *right, int *result, int length);
#endif
storage-layout/symbols.c
int tentative;
int zero = 0;
int initialized = 7;
static int local_zero;
static int local_initialized = 9;
extern int only_declared;
int read_locals(void)
{
return local_zero + local_initialized;
}
strong-common/main.c
#include <stdio.h>
int x = 15213;
void update(void);
int main(void)
{
update();
printf("x = %d\n", x);
return 0;
}
strong-strong/main.c
#include <stdio.h>
int conflict = 1;
int from_other(void);
int main(void)
{
printf("conflict=%d other=%d\n", conflict, from_other());
return 0;
}
weak-function/main.c
#include <stdio.h>
int foo(void);
int main(void)
{
printf("foo() = %d\n", foo());
return 0;
}
weak-undefined/main.c
#include <stdio.h>
extern void optional_hook(void) __attribute__((weak));
int main(void)
{
if (optional_hook != NULL) {
optional_hook();
} else {
puts("optional_hook: absent");
}
return 0;
}
weak-undefined/provider.c
#include <stdio.h>
void optional_hook(void)
{
puts("optional_hook: present");
}
weak-weak/main.c
#include <stdio.h>
extern int choice;
int main(void)
{
printf("choice=%d\n", choice);
return 0;
}