2 * Copyright (c) 2004-2005 Greg Parker. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
50 #include "postlinker.h"
51 #include "symboltable.h"
52 #include "stringtable.h"
58 #define Version "2005-4-14"
60 "Copyright (c) 2004-2005 Greg Parker\n" \
61 "Copyright (c) 2001 David E. O'Brien\n" \
62 "Copyright (c) 1996-1998 John D. Polstra\n" \
63 "All rights reserved.\n" \
65 "Redistribution and use in source and binary forms, with or without\n" \
66 "modification, are permitted provided that the following conditions\n" \
68 "1. Redistributions of source code must retain the above copyright\n" \
69 " notice, this list of conditions and the following disclaimer.\n" \
70 "2. Redistributions in binary form must reproduce the above copyright\n" \
71 " notice, this list of conditions and the following disclaimer in the\n" \
72 " documentation and/or other materials provided with the distribution.\n" \
74 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n" \
75 "ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n" \
76 "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"\
77 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\n" \
78 "FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"\
79 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n" \
80 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n" \
81 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n"\
82 "LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n" \
83 "OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n" \
89 static void version(void)
91 fprintf(stderr, "peal-postlink version %s\n\n", Version);
92 fprintf(stderr, "%s\n", Copyright);
96 static void usage(const char *name)
99 "Usage: %s [-vV] [-K filename] [-t resType] [-s resID] [-o output] filename\n"
100 " -V: print version info\n"
102 " -K <filename>: only keep global symbols listed in file 'filename'\n"
103 " -t <resType>: set resource type for -s (default is 'armc')\n"
104 " -o <output>: write result to file 'output'\n"
105 " -s <resID>: write result in .ro format with one resource per ELF section\n"
106 " (default is .bin format with everything in one resource)\n"
111 int main(int argc, char **argv)
117 const char *infilename;
118 const char *outfilename = NULL;
119 const char *keepfilename = NULL;
120 const char *selfname;
121 vector<string> *keepSymbols = NULL;
122 int printversion = 0;
124 const char *resType = "armc";
128 selfname = strrchr(argv[0], '/');
129 if (!selfname) selfname = argv[0];
131 while ((ch = getopt(argc, argv, "vVK:o:s:t:")) != -1) {
143 fprintf(stderr, "%s: -K may be used only once\n", selfname);
146 keepfilename = optarg;
152 fprintf(stderr, "%s: -o may be used only once\n", selfname);
155 outfilename = optarg;
160 if (splitindex != -1) {
161 fprintf(stderr, "%s: -s may be used only once\n", selfname);
164 splitindex = atoi(optarg);
170 if (strlen(resType) != 4) {
171 fprintf(stderr, "%s: -t resource type must be exactly four characters long\n", selfname);
191 fprintf(stderr, "%s: no file specified\n", selfname);
195 infilename = argv[argc-1];
196 if (!outfilename) outfilename = infilename;
199 // Read keepfile, if any
201 FILE *keepfile = fopen(keepfilename, "r");
202 if (!keepfile) { perror(keepfilename); return 1; }
203 keepSymbols = new vector<string>(0);
207 while ((keep = fgets(buf, sizeof(buf), keepfile))) {
208 char *end = strstr(keep, "\n");
209 if (end) len = end - keep; // skip newline, if any
210 else len = strlen(keep);
211 keepSymbols->push_back(string(keep, len));
216 // Read file into memory
218 fd = open(infilename, O_RDONLY | O_BINARY, 0);
219 if (fd < 0) { perror(infilename); return 1; }
221 err = fstat(fd, &sb);
222 if (err) { perror(infilename); return 1; }
224 uint8_t *buf = (uint8_t *)malloc(sb.st_size);
225 if (sb.st_size != read(fd, buf, sb.st_size)) {
226 perror(infilename); return 1;
231 // Read the ELF image, undo relocations, and record new relocations.
236 // Scrub the symbol table.
237 // - keep all global symbols
238 // - keep a symbol for data and text sections
239 // - fixme apply export list here
241 image.symtab().strip(keepSymbols);
242 image.trimSections(); // removes all string tables
243 image.addSectionGlobals();
244 image.buildSymbolStringTable();
245 image.buildRelocations();
246 image.buildSectionStringTable();
248 image.write(resType, splitindex, outfilename);
250 // fixme paranoia checks:
251 // all ro+alloc+contents sections contiguous (in file and vm)
252 // no ro+alloc-contents sections
253 // all rw+alloc sections coniguous (in file and vm)