Annotation of sys/arch/sparc/stand/common/clean-elf.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: clean-elf.c,v 1.2 2003/11/03 07:01:33 david Exp $ */
2: /*
3: * Public domain. I don't even want my name on this.
4: */
5: #include <stdio.h>
6: #include <stdlib.h>
7: #include <unistd.h>
8: #include <sys/types.h>
9: #include <sys/mman.h>
10: #include <err.h>
11: #include <elf_abi.h>
12: #include <fcntl.h>
13:
14: int
15: elf_is_okay(Elf_Ehdr *ehdr)
16: {
17: int retval = 0;
18:
19: /*
20: * We need to check magic, class size, endianess,
21: * and version before we look at the rest of the
22: * Elf_Ehdr structure. These few elements are
23: * represented in a machine independent fashion.
24: */
25:
26: if (IS_ELF(*ehdr) &&
27: ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
28: ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
29: ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
30: /* Now check the machine dependant header */
31: if (ehdr->e_machine == ELF_TARG_MACH &&
32: ehdr->e_version == ELF_TARG_VER)
33: retval = 1;
34: }
35:
36: return retval;
37: }
38:
39: void
40: cleanit(caddr_t addr)
41: {
42: Elf_Ehdr *ehdr;
43: Elf_Shdr *shdr;
44: char *strtab;
45: int i;
46:
47: ehdr = (Elf_Ehdr *)addr;
48: shdr = (Elf_Shdr *)(addr + ehdr->e_shoff);
49:
50: strtab = shdr[ehdr->e_shstrndx].sh_offset + addr;
51:
52: /*
53: * Simple. find a .text section, verify that the next section is
54: * .rodata and merge them.
55: */
56:
57: for (i = 0; i < ehdr->e_shnum; i++) {
58: Elf_Shdr *t = &shdr[i];
59: Elf_Shdr *r = &shdr[i + 1];
60:
61: #if 0
62: printf("foo: %s %d\n", strtab + t->sh_name, t->sh_type);
63: continue;
64: #endif
65: if (strcmp(strtab + t->sh_name, ".text"))
66: continue;
67: if (strcmp(strtab + r->sh_name, ".rodata"))
68: errx(1, "sorry, rodata merge not possible.");
69:
70: t->sh_size += r->sh_size + (r->sh_offset - t->sh_offset - t->sh_size);
71: r->sh_type = SHT_NULL;
72: r->sh_name = 0;
73: }
74: }
75:
76: int
77: main(int argc, char **argv)
78: {
79: int fd;
80: void *addr;
81: off_t len;
82: int pgsz = getpagesize();
83:
84: if (argc != 2)
85: errx(1, "usage");
86:
87: if ((fd = open(argv[1], O_RDWR)) < 0)
88: err(1, "open");
89:
90: if ((len = lseek(fd, 0, SEEK_END)) < 0)
91: err(1, "lseek");
92:
93: len = ((len + pgsz - 1) & ~(pgsz - 1));
94:
95: if ((addr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) ==
96: MAP_FAILED)
97: err(1, "mmap");
98:
99: if (!elf_is_okay((Elf_Ehdr *)addr))
100: errx(1, "not an elf file");
101:
102: cleanit(addr);
103:
104: msync(addr, len, MS_SYNC|MS_INVALIDATE);
105:
106: close(fd);
107: return (0);
108: }
109:
CVSweb