2 * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/param.h>
18 #include <sys/types.h>
20 #include <sys/exec_elf.h>
38 #define SUNW_CTF ".SUNW_ctf"
40 #define DUMP_OBJECT (1 << 0)
41 #define DUMP_FUNCTION (1 << 1)
42 #define DUMP_HEADER (1 << 2)
44 int dump(const char *, uint32_t);
45 int iself(const char *, size_t);
46 int isctf(const char *, size_t);
47 __dead void usage(void);
49 int ctf_dump(const char *, size_t, uint32_t);
50 const char *ctf_off2name(struct ctf_header *, const char *, off_t,
53 int elf_dump(const char *, size_t, uint32_t);
54 int elf_getshstrtab(const char *, size_t, const char **, size_t *);
55 int elf_getsymtab(const char *, const char *, size_t,
56 const Elf_Sym **, size_t *);
57 int elf_getstrtab(const char *, const char *, size_t,
58 const char **, size_t *);
61 char *decompress(const char *, size_t, off_t);
65 main(int argc, char *argv[])
71 setlocale(LC_ALL, "");
73 while ((ch = getopt(argc, argv, "dfhlsSt")) != -1) {
86 while ((filename = *argv++) != NULL)
87 error |= dump(filename, flags);
93 dump(const char *path, uint32_t flags)
99 fd = open(path, O_RDONLY);
104 if (fstat(fd, &st) == -1) {
108 if (st.st_size < (off_t)sizeof(struct ctf_header)) {
109 warnx("file too small to be CTF");
112 if ((uintmax_t)st.st_size > SIZE_MAX) {
113 warnx("file too big to fit memory");
117 p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
121 if (iself(p, st.st_size)) {
122 error = elf_dump(p, st.st_size, flags);
123 } else if (isctf(p, st.st_size)) {
124 error = ctf_dump(p, st.st_size, flags);
127 munmap(p, st.st_size);
134 iself(const char *p, size_t filesize)
136 Elf_Ehdr *eh = (Elf_Ehdr *)p;
138 if (eh->e_ehsize < sizeof(Elf_Ehdr) || !IS_ELF(*eh)) {
139 warnx("file is not ELF");
142 if (eh->e_ident[EI_CLASS] != ELFCLASS) {
143 warnx("unexpected word size %u", eh->e_ident[EI_CLASS]);
146 if (eh->e_ident[EI_VERSION] != ELF_TARG_VER) {
147 warnx("unexpected version %u", eh->e_ident[EI_VERSION]);
150 if (eh->e_ident[EI_DATA] >= ELFDATANUM) {
151 warnx("unexpected data format %u", eh->e_ident[EI_DATA]);
154 if (eh->e_shoff > filesize) {
155 warnx("bogus section table offset 0x%llx", eh->e_shoff);
158 if (eh->e_shentsize < sizeof(Elf_Shdr)) {
159 warnx("bogus section header size %u", eh->e_shentsize);
162 if (eh->e_shnum > (filesize - eh->e_shoff) / eh->e_shentsize) {
163 warnx("bogus section header count %u", eh->e_shnum);
166 if (eh->e_shstrndx >= eh->e_shnum) {
167 warnx("bogus string table index %u", eh->e_shstrndx);
175 elf_getshstrtab(const char *p, size_t filesize, const char **shstrtab,
176 size_t *shstrtabsize)
178 Elf_Ehdr *eh = (Elf_Ehdr *)p;
181 sh = (Elf_Shdr *)(p + eh->e_shoff + eh->e_shstrndx * eh->e_shentsize);
182 if (sh->sh_type != SHT_STRTAB) {
183 warnx("unexpected string table type");
186 if (sh->sh_offset > filesize) {
187 warnx("bogus string table offset");
190 if (sh->sh_size > filesize - sh->sh_offset) {
191 warnx("bogus string table size");
194 if (shstrtab != NULL)
195 *shstrtab = p + sh->sh_offset;
196 if (shstrtabsize != NULL)
197 *shstrtabsize = sh->sh_size;
203 elf_getsymtab(const char *p, const char *shstrtab, size_t shstrtabsize,
204 const Elf_Sym **symtab, size_t *nsymb)
206 Elf_Ehdr *eh = (Elf_Ehdr *)p;
210 for (i = 0; i < eh->e_shnum; i++) {
211 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
213 if (sh->sh_type != SHT_SYMTAB)
216 if ((sh->sh_link >= eh->e_shnum) ||
217 (sh->sh_name >= shstrtabsize))
220 if (strncmp(shstrtab + sh->sh_name, ELF_SYMTAB,
221 strlen(ELF_SYMTAB)) == 0) {
223 *symtab = (Elf_Sym *)(p + sh->sh_offset);
225 *nsymb = (sh->sh_size / sh->sh_entsize);
235 elf_getstrtab(const char *p, const char *shstrtab, size_t shstrtabsize,
236 const char **strtab, size_t *strtabsize)
238 Elf_Ehdr *eh = (Elf_Ehdr *)p;
242 for (i = 0; i < eh->e_shnum; i++) {
243 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
245 if (sh->sh_type != SHT_STRTAB)
248 if ((sh->sh_link >= eh->e_shnum) ||
249 (sh->sh_name >= shstrtabsize))
252 if (strncmp(shstrtab + sh->sh_name, ELF_STRTAB,
253 strlen(ELF_STRTAB)) == 0) {
255 *strtab = p + sh->sh_offset;
256 if (strtabsize != NULL)
257 *strtabsize = sh->sh_size;
267 const Elf_Sym *symtab;
268 size_t strtabsize, nsymb;
271 elf_dump(const char *p, size_t filesize, uint32_t flags)
273 Elf_Ehdr *eh = (Elf_Ehdr *)p;
275 const char *shstrtab;
276 size_t i, shstrtabsize;
278 /* Find section header string table location and size. */
279 if (elf_getshstrtab(p, filesize, &shstrtab, &shstrtabsize))
282 /* Find symbol table location and number of symbols. */
283 if (elf_getsymtab(p, shstrtab, shstrtabsize, &symtab, &nsymb))
284 warnx("symbol table not found");
286 /* Find string table location and size. */
287 if (elf_getstrtab(p, shstrtab, shstrtabsize, &strtab, &strtabsize))
288 warnx("string table not found");
290 /* Find CTF section and dump it. */
291 for (i = 0; i < eh->e_shnum; i++) {
292 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
294 if ((sh->sh_link >= eh->e_shnum) ||
295 (sh->sh_name >= shstrtabsize))
298 if (strncmp(shstrtab + sh->sh_name, SUNW_CTF, strlen(SUNW_CTF)))
301 if (!isctf(p + sh->sh_offset, sh->sh_size))
304 return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
307 warnx("%s section not found", SUNW_CTF);
312 isctf(const char *p, size_t filesize)
314 struct ctf_header *cth = (struct ctf_header *)p;
315 off_t dlen = cth->cth_stroff + cth->cth_strlen;
317 if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
320 if (dlen > filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
321 warnx("bogus file size");
325 if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
326 (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
327 warnx("wrongly aligned offset");
331 if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
332 (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
333 warnx("truncated file");
337 if ((cth->cth_lbloff > cth->cth_objtoff) ||
338 (cth->cth_objtoff > cth->cth_funcoff) ||
339 (cth->cth_funcoff > cth->cth_typeoff) ||
340 (cth->cth_typeoff > cth->cth_stroff)) {
341 warnx("corrupted file");
349 ctf_dump(const char *p, size_t size, uint32_t flags)
351 struct ctf_header *cth = (struct ctf_header *)p;
352 char *data = (char *)p;
353 off_t dlen = cth->cth_stroff + cth->cth_strlen;
355 if (cth->cth_flags & CTF_F_COMPRESS) {
356 data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
361 if (flags & DUMP_HEADER) {
362 printf("cth_magic = 0x%04x\n", cth->cth_magic);
363 printf("cth_version = %d\n", cth->cth_version);
364 printf("cth_flags = 0x%02x\n", cth->cth_flags);
365 printf("cth_parlabel = %s\n",
366 ctf_off2name(cth, data, dlen, cth->cth_parname));
367 printf("cth_parname = %s\n",
368 ctf_off2name(cth, data, dlen, cth->cth_parname));
369 printf("cth_lbloff = %d\n", cth->cth_lbloff);
370 printf("cth_objtoff = %d\n", cth->cth_objtoff);
371 printf("cth_funcoff = %d\n", cth->cth_funcoff);
372 printf("cth_typeoff = %d\n", cth->cth_typeoff);
373 printf("cth_stroff = %d\n", cth->cth_stroff);
374 printf("cth_strlen = %d\n", cth->cth_strlen);
377 if (cth->cth_flags & CTF_F_COMPRESS)
384 ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
389 if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
392 if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
393 return "exceeds strlab";
395 if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
398 name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
406 decompress(const char *buf, size_t size, off_t len)
419 memset(&stream, 0, sizeof(stream));
420 stream.next_in = (void *)buf;
421 stream.avail_in = size;
422 stream.next_out = data;
423 stream.avail_out = len;
425 if ((error = inflateInit(&stream)) != Z_OK) {
426 warnx("zlib inflateInit failed: %s", zError(error));
430 if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
431 warnx("zlib inflate failed: %s", zError(error));
435 if ((error = inflateEnd(&stream)) != Z_OK) {
436 warnx("zlib inflateEnd failed: %s", zError(error));
440 if (stream.total_out != len) {
441 warnx("decompression failed: %llu != %llu",
442 stream.total_out, len);
457 extern char *__progname;
459 fprintf(stderr, "usage: %s [-dfhlsSt] [file ...]\n",