Check-in [b67cec6988]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Find a DYN ELF file, process a handful of program header entries, locate string table and symbol table, and attempt to "relocate" the PLT. (It just prints out stuff that it would do if it were doing it for realz). Holy hell this is fucking complex. WWAAAAAAAYY more complex than Amiga hunk format. And for what? I just don't see the benefit of any of this.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:b67cec698852e9b26892dea32ca1149c9872ec5cd430bc98f8669b7ceafaa9fd
User & Date: kc5tja 2018-01-29 00:31:46
Context
2018-01-29
08:41
Some minor tweaks Leaf check-in: 5bcf07f30f user: kc5tja tags: trunk
00:31
Find a DYN ELF file, process a handful of program header entries, locate string table and symbol table, and attempt to "relocate" the PLT. (It just prints out stuff that it would do if it were doing it for realz). Holy hell this is fucking complex. WWAAAAAAAYY more complex than Amiga hunk format. And for what? I just don't see the benefit of any of this. check-in: b67cec6988 user: kc5tja tags: trunk
2018-01-28
01:46
Testbin PIE binary check-in: ccfab72945 user: kc5tja tags: trunk
Changes

Added lab/Makefile.













>
>
>
>
>
>
1
2
3
4
5
6
.c.o:
	gcc -c -o $@ $<

lab: lab.o
	gcc -o $@ $<

Added lab/lab.c.





































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdlib.h>
#include <stdio.h>

#include <elf.h>

// These routines exist to make porting to exec.library in the future.

void *
AllocMem(size_t size, int flags) {
	return malloc(size);
}

void
FreeMem(void *p) {
	free(p);
}


struct LoaderContext {
	void *	elfBlob;
};


size_t
GetLengthOfFile(char *filename) {
	FILE *fp;
	size_t fileSize;

	fp = fopen(filename, "rb");
	if(!fp) {
		return -1;
	}
	fseek(fp, 0, SEEK_END);
	fileSize = ftell(fp);
	fclose(fp);

	return fileSize;
}

int
ReadElfBlob(char *filename, void *blob, size_t size) {
	FILE *fp;
	size_t actual;

	fp = fopen(filename, "rb");
	if(!fp) return 0;

	actual = fread(blob, 1, size, fp);
	fclose(fp);

	return actual == size;
}

int
RelocateElfBlob(struct LoaderContext *lc) {
	int i;
	Elf64_Ehdr *ehdr = (Elf64_Ehdr *)lc->elfBlob;
	Elf64_Phdr *phdr = (Elf64_Phdr *)(ehdr->e_phoff + (char *)lc->elfBlob);
	int phdr_count;
	Elf64_Dyn *tag, *taglist;
	int ntags;
	char *stringTable;
	Elf64_Sym *symbolTable;
	Elf64_Addr dt_jmprel = 0;
	Elf64_Xword pltRelocType = 0;
	Elf64_Xword pltRelocSize = 0;

	if(ehdr->e_type != ET_DYN) {
		printf("Not a shared object or PIE file.\n");
		return 0;
	}

	printf("PHDR at %ld (%p)\n", ehdr->e_phoff, phdr);
	phdr_count = ehdr->e_phnum;
	printf("  %d entries in the table.\n", phdr_count);

	/*
	 * To attempt relocation, we must first locate the PT_DYNAMIC
	 * segment.
	 */

	printf("  Looking for PT_DYNAMIC segment: ");
	for(i = 0; i < phdr_count; i++) {
		if(phdr[i].p_type == PT_DYNAMIC) {
			printf("found at entry %d\n", i);
			break;
		}
	}
	if(i == phdr_count) {
		printf("none found.\n");
		return 0;
	}
	printf("    File offset: %p\n", (void *)phdr[i].p_offset);

	/*
	 * Now that we have a PT_DYNAMIC segment, we must now interpret
	 * what it says.  This segment is basically a tag-list in a
	 * slightly less convenient layout than AmigaOS taglists.
	 * We start walking through the segment tag by tag, "executing"
	 * each tag as we find them, in the order we find them.
	 */

	taglist = tag = (Elf64_Dyn *)(phdr[i].p_offset + (char *)lc->elfBlob);
	ntags = phdr[i].p_filesz / sizeof(Elf64_Dyn);

	printf("    Address of first tag: %p\n", tag);
	printf("    Size of taglist: %ld\n", phdr[i].p_filesz);
	printf("    Number of tags alloted: %ld\n", phdr[i].p_filesz / sizeof(Elf64_Dyn));
	printf("    (Number actually used may be less.)\n");

	/*
	 * To resolve DT_NEEDED entries, which lists library dependencies
	 * for this module, wherein we'd recursively load an ELF module if
	 * we were for real, we first need to resolve the DT_STRTAB entry.
	 */

	stringTable = 0;
	symbolTable = NULL;
	for(
		tag = taglist, i = 0;
		i < ntags;
		i++, tag++
	) {
		if(!(tag->d_tag))
			break;

		switch(tag->d_tag) {
		case DT_STRTAB:
			stringTable = (char *)lc->elfBlob + tag->d_un.d_val;
			printf("      String table at %p\n", stringTable);
			break;

		case DT_SYMTAB:
			symbolTable = (Elf64_Sym *)(lc->elfBlob + tag->d_un.d_val);
			printf("      Symbol table at %p\n", symbolTable);
			break;
		}
	}

	/*
	 * Next, resolve dependencies.
	 */

	for(
		tag = taglist, i = 0;
		i < ntags;
		i++, tag++
	) {
		if(!(tag->d_tag))
			break;

		if(tag->d_tag == DT_NEEDED) {
			char *name;
			name = stringTable + tag->d_un.d_val;
			printf("      Dependency: %s\n", name);
			/*
			 * It would be here that we would attempt to check
			 * and make sure the specified dependency was resident
			 * in memory and, if not, attempt to recursively load
			 * the dependency as an ELF module unto itself.
			 */
		}
	}

	/*
	 * Next, relocate PLT entries.  Either all three tags should be present,
	 * or none of them at all.  We must also be sensitive to the specific
	 * type of relocation records specified.
	 */

	for(
		tag = taglist, i = 0;
		i < ntags;
		i++, tag++
	) {
		if(!(tag->d_tag))
			break;

		switch(tag->d_tag) {
		case DT_JMPREL:
			dt_jmprel = tag->d_un.d_ptr;
			break;

		case DT_PLTRELSZ:
			pltRelocSize = tag->d_un.d_val;
			break;

		case DT_PLTREL:
			pltRelocType = tag->d_un.d_val;
			break;
		}
	}

	if(!dt_jmprel && (pltRelocType || pltRelocSize)) {
		printf("I'm missing the DT_JMPREL tag.\n");
		return 0;
	}

	if(!pltRelocType && (dt_jmprel || pltRelocSize)) {
		printf("I'm missing the DT_PLTREL tag.\n");
		return 0;
	}

	if(!pltRelocSize && (dt_jmprel || pltRelocType)) {
		printf("I'm missing the DT_PLTRELSZ tag.\n");
		return 0;
	}

	if(!dt_jmprel && !pltRelocType && !pltRelocSize) {
		printf("      NO PLT RELOCATIONS.\n");
	}
	else if(pltRelocType == DT_REL) {
		Elf64_Rel *pltRelocations = (Elf64_Rel *)((char *)lc->elfBlob + dt_jmprel);
		printf("      PLT REL Relocations at %p\n", pltRelocations);
		while(pltRelocSize) {
			printf(
				"        Relocation at %p of type %08lX\n",
				pltRelocations->r_offset + lc->elfBlob,
				pltRelocations->r_info
			);
			pltRelocSize -= sizeof(Elf64_Rel);
			pltRelocations++;
		}
	}
	else if(pltRelocType == DT_RELA) {
		Elf64_Rela *pltRelocations = (Elf64_Rela *)((char *)lc->elfBlob + dt_jmprel);
		printf("      PLT RELA Relocations at %p\n", pltRelocations);
		while(pltRelocSize) {
			int symbolKey = (int)ELF64_R_SYM(pltRelocations->r_info);
			int relocationType = (int)ELF64_R_TYPE(pltRelocations->r_info);
			Elf64_Sym *symbol = &symbolTable[symbolKey];

			printf(
				"        Relocation at %p of symbol %d (%s) of type %d, addend %016lX\n",
				pltRelocations->r_offset + lc->elfBlob,
				symbolKey,
				symbol->st_name ? stringTable + symbol->st_name : "<anonymous>",
				relocationType,
				pltRelocations->r_addend
			);
			pltRelocSize -= sizeof(Elf64_Rela);
			pltRelocations++;
		}
	}

	return 1;
}

void
cleanExit(char *reason, struct LoaderContext *lc) {
	int code = 0;

	if(lc) {
		if(lc->elfBlob)		FreeMem(lc->elfBlob);
		FreeMem(lc);
	}

	if(reason) {
		printf("ERROR: %s\n", reason);
		code = 1;
	}

	exit(code);
}

void
main(int argc, char *argv[]) {
	char *inputFile = NULL;
	size_t lengthOfElf;
	struct LoaderContext *lc;

	printf("ElfLab V1.0\n");

	if(argc < 2) {
		printf("I need a file to parse.\n");
		exit(1);
	}
	inputFile = argv[1];

	lengthOfElf = GetLengthOfFile(inputFile);
	printf("ELF filename: %s\nELF filesize: %ld\n", inputFile, lengthOfElf);

	/*
	 * Read the ELF file into memory, completely as-is.
	 * Unlike Amiga Hunk files, ELFs are not parseable with recursive-
	 * descent parsers.  Instead, ELFs form a DAG of objects.  Reading
	 * the whole blob at once makes traversing this graph easier.
	 *
	 * You might be tempted to relocate in-place.  For code, this seems
	 * reasonable; however, it won't work for data segments.  The reason
	 * is that data segments can be larger than what appears in the file.
	 * TECHNICALLY, this is true of text segments as well, but it's
	 * exceedingly rare for that to happen.
	 */

	lc = AllocMem(sizeof(struct LoaderContext), 0);
	if(!lc)
		cleanExit("LoaderContext", lc);

	lc->elfBlob = AllocMem(lengthOfElf, 0);
	if(!lc->elfBlob)
		cleanExit("AllocMem", lc);

	if(!ReadElfBlob(inputFile, lc->elfBlob, lengthOfElf))
		cleanExit("ReadElfBlob", lc);

	/*
	 * At this point, our ELF image is now in RAM, occupying a single
	 * block of RAM.  The image will contain all the binary data and
	 * code that would normally exist in a program.  The problem is,
	 * none of the references in the code has any idea where anything
	 * else sits in memory.  So, we need to apply fixups and relocations
	 * to the binary as it currently exists, inserting tabs into
	 * corresponding slots, so to speak.
	 */

	if(!RelocateElfBlob(lc))
		cleanExit("RelocateElfBlob", lc);

	exit(0);
}