Deleted Added
sdiff udiff text old ( 246278 ) new ( 246296 )
full compact
1/*
2 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 20 unchanged lines hidden (view full) ---

29 */
30
31#include <sys/cdefs.h>
32#ifndef lint
33#if 0
34__RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
35#endif
36#endif
37__FBSDID("$FreeBSD: head/usr.sbin/crunch/crunchide/exec_elf32.c 246278 2013-02-03 01:54:25Z pfg $");
38
39#ifndef ELFSIZE
40#define ELFSIZE 32
41#endif
42
43#include <sys/types.h>
44#include <sys/endian.h>
45#include <sys/stat.h>
46
47#include <errno.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53#include "extern.h"
54
55#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \

--- 21 unchanged lines hidden (view full) ---

77#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
78#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
79#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
80
81#define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
82#define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
83#define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
84
85struct listelem {
86 struct listelem *next;
87 void *mem;
88 off_t file;
89 size_t size;
90};
91
92static ssize_t
93xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
94{
95 ssize_t rv;
96
97 if (lseek(fd, off, SEEK_SET) != off) {

--- 132 unchanged lines hidden (view full) ---

230 * The new renaming behaviour doesn't take global symbols out of the
231 * namespace. However, it's ... unlikely that there will ever be
232 * any collisions in practice because of the new method.
233 */
234int
235ELFNAMEEND(hide)(int fd, const char *fn)
236{
237 Elf_Ehdr ehdr;
238 Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr;
239 Elf_Sym *symtabp = NULL;
240 char *strtabp = NULL;
241 Elf_Size nsyms, ewi;
242 ssize_t shdrsize;
243 int rv, i, weird;
244 size_t nstrtab_size, nstrtab_nextoff, fn_size;
245 char *nstrtabp = NULL;
246 unsigned char data;
247 Elf_Off maxoff, stroff;
248 const char *weirdreason = NULL;
249
250 rv = 0;
251 if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
252 goto bad;
253
254 data = ehdr.e_ident[EI_DATA];
255
256 shdrsize = xe16toh(ehdr.e_shnum) * xe16toh(ehdr.e_shentsize);
257 if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
258 goto bad;
259 if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
260 shdrsize)
261 goto bad;
262
263 symtabshdr = strtabshdr = NULL;
264 weird = 0;
265 maxoff = stroff = 0;
266 for (i = 0; i < xe16toh(ehdr.e_shnum); i++) {
267 if (xewtoh(shdrp[i].sh_offset) > maxoff)
268 maxoff = xewtoh(shdrp[i].sh_offset);
269 switch (xe32toh(shdrp[i].sh_type)) {
270 case SHT_SYMTAB:
271 if (symtabshdr != NULL)
272 weird = 1;
273 symtabshdr = &shdrp[i];
274 strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
275
276 /* Check whether the string table is the last section */
277 stroff = xewtoh(shdrp[xe32toh(shdrp[i].sh_link)].sh_offset);
278 if (!weird && xe32toh(shdrp[i].sh_link) != (xe16toh(ehdr.e_shnum) - 1)) {
279 weird = 1;
280 weirdreason = "string table not last section";
281 }
282 break;
283 }
284 }
285 if (! weirdreason)
286 weirdreason = "unsupported";
287 if (symtabshdr == NULL)
288 goto out;
289 if (strtabshdr == NULL)
290 weird = 1;
291 if (!weird && stroff != maxoff) {
292 weird = 1;
293 weirdreason = "string table section not last in file";
294 }
295 if (weird) {
296 fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
297 goto bad;
298 }
299
300 /*
301 * load up everything we need
302 */
303
304 /* symbol table */
305 if ((symtabp = xmalloc(xewtoh(symtabshdr->sh_size), fn, "symbol table"))
306 == NULL)
307 goto bad;
308 if ((size_t)xreadatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
309 xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
310 goto bad;
311
312 /* string table */
313 if ((strtabp = xmalloc(xewtoh(strtabshdr->sh_size), fn, "string table"))
314 == NULL)
315 goto bad;
316 if ((size_t)xreadatoff(fd, strtabp, xewtoh(strtabshdr->sh_offset),
317 xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
318 goto bad;
319
320 nstrtab_size = 256;
321 nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
322 if (nstrtabp == NULL)
323 goto bad;
324 nstrtab_nextoff = 0;
325
326 fn_size = strlen(fn);
327

--- 32 unchanged lines hidden (view full) ---

360 newent_len = sprintf(nstrtabp + nstrtab_nextoff,
361 "_$$hide$$ %s %s", fn, symname) + 1;
362 }
363 nstrtab_nextoff += newent_len;
364 }
365 strtabshdr->sh_size = htoxew(nstrtab_nextoff);
366
367 /*
368 * write new tables to the file
369 */
370 if (xwriteatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
371 shdrsize)
372 goto bad;
373 if ((size_t)xwriteatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
374 xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
375 goto bad;
376 /* write new symbol table strings */
377 if ((size_t)xwriteatoff(fd, nstrtabp, xewtoh(strtabshdr->sh_offset),
378 xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
379 goto bad;
380
381out:
382 if (shdrp != NULL)
383 free(shdrp);
384 if (symtabp != NULL)
385 free(symtabp);
386 if (strtabp != NULL)
387 free(strtabp);
388 if (nstrtabp != NULL)
389 free(nstrtabp);
390 return (rv);
391
392bad:
393 rv = 1;
394 goto out;
395}
396
397#endif /* include this size of ELF */