CSAPP Wiki

Ch7 ELF 실습

실습 설명을 읽고 소스를 확인한다. 원본 파일 링크는 소스 텍스트를 연다.

archive-cycle/main.c

원본 파일

C
#include <stdio.h>

extern int x(int value);

int main(void)
{
    printf("x(3) = %d\n", x(3));
    return 0;
}

archive-cycle/x.c

원본 파일

C
extern int y(int value);

int x(int value)
{
    return y(value) + 1;
}

archive-cycle/x_helper.c

원본 파일

C
int x_helper(int value)
{
    return value * 2;
}

archive-cycle/y.c

원본 파일

C
extern int x_helper(int value);

int y(int value)
{
    return x_helper(value) + 10;
}

aslr-pie/addresses.c

원본 파일

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

원본 파일

C
#include <stdio.h>

int x;
void update(void);

int main(void)
{
    x = 15213;
    update();
    printf("x = %d\n", x);
    return 0;
}

common-common/worker.c

원본 파일

C
int x;

void update(void)
{
    x = 15212;
}

common-mismatch/main.c

원본 파일

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-mismatch/worker.c

원본 파일

C
double x;

void overwrite(void)
{
    x = -0.0;
}

common-size/large.c

원본 파일

C
char arena[32];

common-size/main.c

원본 파일

C
#include <stdio.h>

extern char arena[];

int main(void)
{
    arena[31] = 42;
    printf("arena[31]=%d\n", arena[31]);
    return 0;
}

common-size/small.c

원본 파일

C
char arena[4];

compiler-driver/main.c

원본 파일

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;
}

compiler-driver/utils.c

원본 파일

C
int foo(int value)
{
    return value * value * value;
}

duplicate-function/main.c

원본 파일

C
int helper(void)
{
    return 1;
}

int main(void)
{
    return helper() != 1;
}

duplicate-function/other.c

원본 파일

C
int helper(void)
{
    return 2;
}

explicit-weak/main.c

원본 파일

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

원본 파일

C
int hook __attribute__((weak)) = 3;

int provider_value(void)
{
    return hook;
}

lto-dead-code/main.c

원본 파일

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

원본 파일

C
int cube(int x)
{
    return x * x * x;
}

int unused_helper(int x)
{
    return x + 1000;
}

static-internal/main.c

원본 파일

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-internal/other.c

원본 파일

C
static int x = 22;

int other_value(void)
{
    return x;
}

static-library/addvec.c

원본 파일

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

원본 파일

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

원본 파일

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

원본 파일

C
#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

원본 파일

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

원본 파일

C
#include <stdio.h>

int x = 15213;
void update(void);

int main(void)
{
    update();
    printf("x = %d\n", x);
    return 0;
}

strong-common/worker.c

원본 파일

C
int x;

void update(void)
{
    x = 15212;
}

strong-strong/main.c

원본 파일

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;
}

strong-strong/other.c

원본 파일

C
int conflict = 2;

int from_other(void)
{
    return conflict;
}

weak-function/default.c

원본 파일

C
int foo(void) __attribute__((weak));

int foo(void)
{
    return 1;
}

weak-function/main.c

원본 파일

C
#include <stdio.h>

int foo(void);

int main(void)
{
    printf("foo() = %d\n", foo());
    return 0;
}

weak-function/override.c

원본 파일

C
int foo(void)
{
    return 2;
}

weak-undefined/main.c

원본 파일

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

원본 파일

C
#include <stdio.h>

void optional_hook(void)
{
    puts("optional_hook: present");
}

weak-weak/left.c

원본 파일

C
int choice __attribute__((weak)) = 11;

weak-weak/main.c

원본 파일

C
#include <stdio.h>

extern int choice;

int main(void)
{
    printf("choice=%d\n", choice);
    return 0;
}

weak-weak/right.c

원본 파일

C
int choice __attribute__((weak)) = 22;