Skip to content

Commit

Permalink
Examples until p.168
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoski committed Sep 1, 2013
1 parent 0f78bed commit ef251b1
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PROGS = ab a.o b.o ManualNameMangling SimpleSection.o SpecialSymbol \
target TinyHelloWorld SectionMapping.elf

all: $(PROGS)

clean:
rm -f $(PROGS)

ab: a.o b.o
$(LD) a.o b.o -e main -o ab

ManualNameMangling: ManualNameMangling.cc
$(CXX) ManualNameMangling.cc -o ManualNameMangling

SpecialSymbol: SpecialSymbol.c
$(CC) $< -o $@

target: target.c
$(CC) -o $@ $< -lbfd

TinyHelloWorld: TinyHelloWorld.c TinyHelloWorld.lds
$(CC) -c -fno-builtin TinyHelloWorld.c
$(LD) -static -T TinyHelloWorld.lds -o $@ TinyHelloWorld.o

SectionMapping.elf: SectionMapping.c
$(CC) -static $< -o $@
12 changes: 12 additions & 0 deletions ManualNameMangling.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

namespace myname {
int var = 42;
}

extern "C" int _ZN6myname3varE;

int main() {
printf("%d\n", _ZN6myname3varE);
return 0;
}
7 changes: 7 additions & 0 deletions SectionMapping.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdlib.h>

int main() {
while(1)
sleep(1000);
return 0;
}
20 changes: 20 additions & 0 deletions SimpleSection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int printf(const char *format, ...);

int global_init_var = 84;
int global_uninit_var;

void func1(int i) {
printf("%d\n", i);
}

int main(void) {
static int static_var = 85;
static int staic_var2;

int a = 1;
int b;

func1(static_var + staic_var2 + a + b);

return a;
}
14 changes: 14 additions & 0 deletions SpecialSymbol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

extern char *__executable_start;
extern char *etext, *_etext, *__etext;
extern char *edata, *_edata;
extern char *end, *_end;

int main() {
printf("Executable start %X\n", __executable_start);
printf("Text end %X %X %X\n", etext, _etext, __etext);
printf("Data end %X %X\n", edata, _edata);
printf("Executable end %X %X\n", end, _end);
return 0;
}
21 changes: 21 additions & 0 deletions TinyHelloWorld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
char *str = "Hello World!\n";

void print() {
asm("movl $13, %%edx\n\t" // length
"movl %%eax, %%ecx\n\t" // str
"movl $0, %%ebx\n\t" // STDOUT
"movl $4, %%eax\n\t" // write
"int $0x80\n\t"
:: "r" (str) : "edx", "ecx", "ebx");
}

void exit() {
asm("movl $42, %ebx\n\t" // exit code
"movl $1, %eax\n\t" // exit
"int $0x80\n\t");
}

void nomain() {
print();
exit();
}
8 changes: 8 additions & 0 deletions TinyHelloWorld.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ENTRY(nomain)

SECTIONS
{
. = 0x00400000 + SIZEOF_HEADERS;
tinytext : { *(.text) *(.data) *(.rodata) }
/DISCARD/ : { *(.comment) }
}
6 changes: 6 additions & 0 deletions a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern int shared;

int main() {
int a = 100;
swap(&a, &shared);
}
5 changes: 5 additions & 0 deletions b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int shared = 1;

void swap(int *a, int *b) {
*a ^= *b ^= *a ^= *b;
}
7 changes: 7 additions & 0 deletions strongWeek.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int weak;
int strong = 1;
__attribute__((weak)) weak2 = 2;

int main() {
return 0;
}
9 changes: 9 additions & 0 deletions target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <bfd.h>

int main() {
const char **t = bfd_target_list();
while(*t) {
puts(*t++);
}
}
14 changes: 14 additions & 0 deletions weakref.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <pthread.h>

int pthread_create(pthread_t *, const pthread_attr_t *,
void *(*) (void *), void *) __attribute__ ((weak));

int main() {
if(pthread_create) {
puts("This is multi-threaded version.");
} else {
puts("This is single-threaded version.");
}
return 0;
}

0 comments on commit ef251b1

Please sign in to comment.