aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/elfdl386.c
blob: a67ed13cfe8018d464771e3813bb9c67b2f3b70f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <u.h>
#include <libc.h>
#include <mach.h>
#include "elf.h"

/*
aggr Linkdebug
{
	'X' 0 version;
	'X' 4 map;
};

aggr Linkmap
{
	'X' 0 addr;
	'X' 4 name;
	'X' 8 dynsect;
	'X' 12 next;
	'X' 16 prev;
};
*/
enum
{
	DT_NULL = 0,
	DT_NEEDED,
	DT_PLTRRELSZ,
	DT_PLTGOT,
	DT_HASH,
	DT_STRTAB,
	DT_SYMTAB,
	DT_RELA,
	DT_RELASZ = 8,
	DT_RELAENT,
	DT_STSZ,
	DT_SYMENT,
	DT_INIT,
	DT_FINI,
	DT_SONAME,
	DT_RPATH,
	DT_SYMBOLIC = 16,
	DT_REL,
	DT_RELSZ,
	DT_RELENT,
	DT_PLTREL,
	DT_DEBUG,
	DT_TEXTREL,
	DT_JMPREL
};

static int
getstr(Map *map, ulong addr, char *buf, uint nbuf)
{
	int i;

	for(i=0; i<nbuf; i++){
		if(get1(map, addr+i, (uchar*)buf+i, 1) < 0)
			return -1;
		if(buf[i] == 0)
			return 0;
	}
	return -1;	/* no nul */
}

static ulong
dyninfo(Fhdr *hdr, int x)
{
	u32int addr, u;

	if(hdr == nil || (addr = ((Elf*)hdr->elf)->dynamic) == 0){
		fprint(2, "no hdr/dynamic %p\n", hdr);
		return 0;
	}
	addr += hdr->base;

	while(addr != 0){
		if(get4(cormap, addr, &u) < 0)
			return 0;
		if(u == x){
			if(get4(cormap, addr+4, &u) < 0)
				return 0;
			return u;
		}
		addr += 8;
	}
	return 0;
}

void
elfdl386mapdl(int verbose)
{
	int i;
	Fhdr *hdr;
	u32int linkdebug, linkmap, name, addr;
	char buf[1024];

	if((linkdebug = dyninfo(symhdr, DT_DEBUG)) == 0){
		fprint(2, "no dt_debug section\n");
		return;
	}
	if(get4(cormap, linkdebug+4, &linkmap) < 0){
		fprint(2, "get4 linkdebug+4 (0x%lux) failed\n", linkdebug);
		return;
	}

	for(i=0; i<100 && linkmap != 0; i++){
		if(get4(cormap, linkmap, &addr) < 0
		|| get4(cormap, linkmap+4, &name) < 0
		|| get4(cormap, linkmap+12, &linkmap) < 0)
			break;

		if(name == 0 || getstr(cormap, name, buf, sizeof buf) < 0 || buf[0] == 0)
			continue;
		if((hdr = crackhdr(buf, OREAD)) == nil){
			fprint(2, "crackhdr %s: %r\n", buf);
			continue;
		}
		hdr->base = addr;
		if(verbose)
			fprint(2, "%s: %s %s %s\n", buf, hdr->aname, hdr->mname, hdr->fname);
		if(mapfile(hdr, addr, symmap, nil) < 0)
			fprint(2, "mapping %s: %r\n", buf);
		if(corhdr){
			/*
			 * Need to map the text file under the core file.
			 */
			unmapfile(corhdr, cormap);
			mapfile(hdr, addr, cormap, nil);
			mapfile(corhdr, 0, cormap, nil);
		}
		if(symopen(hdr) < 0)
			fprint(2, "syminit %s: %r\n", buf);
	}
}