Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * vdso2c - A vdso image preparation tool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2014 Andy Lutomirski and others
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Licensed under the GPL v2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * vdso2c requires stripped and unstripped input.  It would be trivial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * to fully strip the input in here, but, for reasons described below,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * we need to write a section table.  Doing this is more or less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * equivalent to dropping all non-allocatable sections, but it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * easier to let objcopy handle that instead of doing it ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * If we ever need to do something fancier than what objcopy provides,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * it would be straightforward to add here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * We're keep a section table for a few reasons:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * The Go runtime had a couple of bugs: it would read the section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * table to try to figure out how many dynamic symbols there were (it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * shouldn't have looked at the section table at all) and, if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * were no SHT_SYNDYM section table entry, it would use an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * uninitialized value for the number of symbols.  An empty DYNSYM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * table would work, but I see no reason not to write a valid one (and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * keep full performance for old Go programs).  This hack is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * needed on x86_64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * The bug was introduced on 2012-08-31 by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * https://code.google.com/p/go/source/detail?r=56ea40aac72b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * and was fixed on 2014-06-13 by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * https://code.google.com/p/go/source/detail?r=fc1cd5e12595
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Binutils has issues debugging the vDSO: it reads the section table to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * would break build-id if we removed the section table.  Binutils
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * also requires that shstrndx != 0.  See:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * elfutils might not look for PT_NOTE if there is a section table at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * all.  I don't know whether this matters for any practical purpose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * For simplicity, rather than hacking up a partial section table, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * just write a mostly complete one.  We omit non-dynamic symbols,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * though, since they're rather large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * Once binutils gets fixed, we might be able to drop this for all but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * the 64-bit vdso, since build-id only works in kernel RPMs, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * systems that update to new enough kernel RPMs will likely update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * binutils in sync.  build-id has never worked for home-built kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * RPMs without manual symlinking, and I suspect that no one ever does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #include <tools/le_byteshift.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) const char *outfilename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /* Symbols that we need in vdso2c. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	sym_vvar_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	sym_vvar_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	sym_pvclock_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	sym_hvclock_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	sym_timens_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) const int special_pages[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	sym_vvar_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	sym_pvclock_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	sym_hvclock_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	sym_timens_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) struct vdso_sym {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	bool export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct vdso_sym required_syms[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	[sym_vvar_start] = {"vvar_start", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	[sym_vvar_page] = {"vvar_page", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	[sym_pvclock_page] = {"pvclock_page", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	[sym_hvclock_page] = {"hvclock_page", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	[sym_timens_page] = {"timens_page", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	{"VDSO32_NOTE_MASK", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	{"__kernel_vsyscall", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	{"__kernel_sigreturn", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	{"__kernel_rt_sigreturn", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	{"int80_landing_pad", true},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void fail(const char *format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	va_start(ap, format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	fprintf(stderr, "Error: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	vfprintf(stderr, format, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (outfilename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		unlink(outfilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * Evil macros for little-endian reads and writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define GLE(x, bits, ifnot)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	__builtin_choose_expr(						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		(sizeof(*(x)) == bits/8),				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		(__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) extern void bad_get_le(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define LAST_GLE(x)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define GET_LE(x)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define PLE(x, val, bits, ifnot)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	__builtin_choose_expr(						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		(sizeof(*(x)) == bits/8),				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		put_unaligned_le##bits((val), (x)), ifnot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) extern void bad_put_le(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define LAST_PLE(x, val)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define PUT_LE(x, val)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define NSYMS ARRAY_SIZE(required_syms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define BITSFUNC3(name, bits, suffix) name##bits##suffix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define ELF_BITS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #include "vdso2c.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #undef ELF_BITS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define ELF_BITS 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #include "vdso2c.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #undef ELF_BITS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void go(void *raw_addr, size_t raw_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	       void *stripped_addr, size_t stripped_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	       FILE *outfile, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		go64(raw_addr, raw_len, stripped_addr, stripped_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		     outfile, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	} else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		go32(raw_addr, raw_len, stripped_addr, stripped_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		     outfile, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		fail("unknown ELF class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void map_input(const char *name, void **addr, size_t *len, int prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	off_t tmp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int fd = open(name, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (fd == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		err(1, "open(%s)", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	tmp_len = lseek(fd, 0, SEEK_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (tmp_len == (off_t)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		err(1, "lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	*len = (size_t)tmp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	*addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (*addr == MAP_FAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		err(1, "mmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	size_t raw_len, stripped_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	void *raw_addr, *stripped_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	FILE *outfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	char *name, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (argc != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	 * Figure out the struct name.  If we're writing to a .so file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	 * generate raw output insted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	name = strdup(argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	namelen = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		tmp = strrchr(name, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			name = tmp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		tmp = strchr(name, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			*tmp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		for (tmp = name; *tmp; tmp++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			if (*tmp == '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				*tmp = '_';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	outfilename = argv[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	outfile = fopen(outfilename, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!outfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		err(1, "fopen(%s)", outfilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	munmap(raw_addr, raw_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	munmap(stripped_addr, stripped_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	fclose(outfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }