Skip to content

Commit 3741031

Browse files
committed
Add wrapper for llvm-objcopy
1 parent 6be384c commit 3741031

7 files changed

Lines changed: 558 additions & 8 deletions

File tree

tools/gcc-wrapper/Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SOURCES = biggestint.c \
1818
strsplit.c \
1919
urldecode.c \
2020
urlencode.c \
21+
walkdir.c \
2122
fs/absoluteness.c \
2223
fs/basename.c \
2324
fs/cp.c \
@@ -28,15 +29,16 @@ SOURCES = biggestint.c \
2829
fs/parentpath.c \
2930
fs/realpath.c \
3031
fs/stripsep.c \
32+
fs/rm.c \
3133
os/find_exe.c
3234

33-
GCC_SOURCES = $(SOURCES) main.c
34-
STRIP_SOURCES = $(SOURCES) strip.c
35+
GCC_SOURCES = $(SOURCES) gcc.c
36+
STRIP_SOURCES = $(SOURCES) binutils.c
3537

3638
gcc:
3739
$(CC) -D $(FLAVOR) -I $(SOURCE_DIRECTORY) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) $(GCC_SOURCES) -o $(PREFIX)/gcc-wrapper
3840

39-
strip:
40-
$(CC) -D $(FLAVOR) -I $(SOURCE_DIRECTORY) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) $(STRIP_SOURCES) -o $(PREFIX)/strip-wrapper
41+
binutils:
42+
$(CC) -D $(FLAVOR) -I $(SOURCE_DIRECTORY) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) $(STRIP_SOURCES) -o $(PREFIX)/binutils-wrapper
4143

42-
all: gcc strip
44+
all: gcc binutils
Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
#include "fs/getexec.h"
1111
#include "fs/parentpath.h"
12+
#include "fs/basename.h"
13+
#include "fs/exists.h"
1214
#include "fs/sep.h"
15+
#include "fs/rm.h"
1316
#include "fstream.h"
1417
#include "errors.h"
1518
#include "obggcc.h"
@@ -20,6 +23,13 @@ extern char** environ;
2023
static const char HYPHEN[] = "-";
2124
static const char BINUTILS_STRIP[] = "strip";
2225

26+
static const char LLVM_STRIP[] = "llvm-strip";
27+
static const char LLVM_OBJCOPY[] = "llvm-objcopy";
28+
29+
static const char LLVM_COMMAND_PREFIX[] = "llvm-";
30+
31+
static const char LIBCXX_SHARED[] = "libc++_shared.so";
32+
2333
#define BINFMT_X86_64 (0x01)
2434
#define BINFMT_i386 (0x02)
2535
#define BINFMT_MIPS64EL (0x03)
@@ -187,6 +197,105 @@ static const char* binfmt_get_triplet(const struct binfmt* const fmt) {
187197

188198
}
189199

200+
static int ends_with(const char* const string, const char* const sep) {
201+
202+
const size_t asize = strlen(string);
203+
const size_t bsize = strlen(sep);
204+
205+
const char* pos = string;
206+
207+
if (bsize > asize) {
208+
return 0;
209+
}
210+
211+
pos += (asize - bsize);
212+
213+
return strcmp(pos, sep) == 0;
214+
215+
}
216+
217+
static const char* const ARGUMENT_EXPECTS_VALUE[] = {
218+
"-o",
219+
"--keep-section",
220+
"--remove-relocations",
221+
"--add-gnu-debuglink",
222+
"--keep-section",
223+
"--interleave-width",
224+
"--gap-fill",
225+
"--pad-to",
226+
"--set-start",
227+
"-K", "--keep-symbol",
228+
"-N", "--strip-symbol",
229+
"-R", "--remove-section",
230+
"-I", "--input-target",
231+
"-O", "--output-target",
232+
"-F", "--target",
233+
"-j", "--only-section",
234+
"-L", "--localize-symbol",
235+
"-G", "--keep-global-symbol",
236+
"-W", "--weaken-symbol",
237+
"-i", "--interleave",
238+
"-b", "--byte",
239+
"--change-start", "--adjust-start",
240+
"--change-addresses", "--adjust-vma",
241+
"--change-section-address", "--adjust-section-vma",
242+
"--change-section-lma",
243+
"--change-section-vma",
244+
"--set-section-flags",
245+
"--set-section-alignment",
246+
"--add-section",
247+
"--update-section",
248+
"--dump-section",
249+
"--rename-section",
250+
"--long-section-names",
251+
"--reverse-bytes",
252+
"--redefine-sym",
253+
"--redefine-syms",
254+
"--srec-len",
255+
"--strip-symbols",
256+
"--strip-unneeded-symbols",
257+
"--keep-symbols",
258+
"--localize-symbols",
259+
"--globalize-symbols",
260+
"--keep-global-symbols",
261+
"--weaken-symbols",
262+
"--add-symbol",
263+
"--alt-machine-code",
264+
"--prefix-symbols",
265+
"--prefix-sections",
266+
"--prefix-alloc-sections",
267+
"--file-alignment",
268+
"--heap",
269+
"--image-base",
270+
"--section-alignment",
271+
"--stack",
272+
"--subsystem",
273+
"--compress-debug-sections",
274+
"--elf-stt-common",
275+
"--verilog-data-width"
276+
};
277+
278+
static int argument_expects_value(const char* const name) {
279+
280+
size_t index = 0;
281+
const char* item = NULL;
282+
283+
if (name == NULL) {
284+
return 0;
285+
}
286+
287+
for (index = 0; index < sizeof(ARGUMENT_EXPECTS_VALUE) / sizeof(*ARGUMENT_EXPECTS_VALUE); index++) {
288+
item = ARGUMENT_EXPECTS_VALUE[index];
289+
290+
if (strcmp(name, item) == 0) {
291+
return 1;
292+
}
293+
}
294+
295+
return 0;
296+
297+
}
298+
190299
int main(int argc, char* argv[], char* envp[]) {
191300

192301
hquery_t query = {0};
@@ -201,8 +310,10 @@ int main(int argc, char* argv[], char* envp[]) {
201310
const struct binfmt* fmt = NULL;
202311

203312
const char* triplet = NULL;
313+
const char* output = NULL;
204314
char* input = NULL;
205315

316+
const char* file_name = NULL;
206317
char* parent_directory = NULL;
207318
char* app_filename = NULL;
208319

@@ -246,7 +357,15 @@ int main(int argc, char* argv[], char* envp[]) {
246357
for (index = 1; index < (size_t) argc; index++) {
247358
cur = argv[index];
248359

249-
if (cur[0] == '-' || prev != NULL && strcmp(prev, "-o") == 0) {
360+
if (ends_with(cur, ".sym") && file_exists(cur) != 1) {
361+
continue;
362+
}
363+
364+
if (prev != NULL && strcmp(prev, "-o") == 0) {
365+
output = cur;
366+
}
367+
368+
if (ends_with(cur, ".sym") || cur[0] == '-' || argument_expects_value(prev)) {
250369
kargv[kargc++] = cur;
251370
} else {
252371
inputs[inputsc++] = cur;
@@ -269,6 +388,15 @@ int main(int argc, char* argv[], char* envp[]) {
269388
goto end;
270389
}
271390

391+
file_name = basename(app_filename);
392+
393+
if (!(strcmp(file_name, LLVM_STRIP) == 0 || strcmp(file_name, LLVM_OBJCOPY) == 0)) {
394+
err = ERR_UNKNOWN_BINUTILS_WRAPPER;
395+
goto end;
396+
}
397+
398+
file_name += strlen(LLVM_COMMAND_PREFIX);
399+
272400
parent_directory = malloc(strlen(app_filename) + 1);
273401

274402
if (parent_directory == NULL) {
@@ -314,7 +442,7 @@ int main(int argc, char* argv[], char* envp[]) {
314442

315443
free(executable);
316444

317-
executable = malloc(strlen(parent_directory) + strlen(PATHSEP_S) + strlen(triplet) + strlen(HYPHEN) + strlen(BINUTILS_STRIP) + 1);
445+
executable = malloc(strlen(parent_directory) + strlen(PATHSEP_S) + strlen(triplet) + strlen(HYPHEN) + strlen(file_name) + 1);
318446

319447
if (executable == NULL) {
320448
err = ERR_MEM_ALLOC_FAILURE;
@@ -325,7 +453,7 @@ int main(int argc, char* argv[], char* envp[]) {
325453
strcat(executable, PATHSEP_S);
326454
strcat(executable, triplet);
327455
strcat(executable, HYPHEN);
328-
strcat(executable, BINUTILS_STRIP);
456+
strcat(executable, file_name);
329457

330458
args[0] = executable;
331459
args[kargc] = input;
@@ -342,6 +470,8 @@ int main(int argc, char* argv[], char* envp[]) {
342470
fprintf(stderr, "fatal error: %s: %s\n", obggcc_strerror(ERR_EXECVE_FAILURE), strerror(errno));
343471
_exit(EXIT_FAILURE);
344472
}
473+
474+
_exit(EXIT_SUCCESS);
345475
} else if (pid > 0) {
346476
wait(&wstatus);
347477
wstatus = WIFSIGNALED(wstatus) ? 128 + WTERMSIG(wstatus) : WEXITSTATUS(wstatus);
@@ -354,6 +484,21 @@ int main(int argc, char* argv[], char* envp[]) {
354484
err = ERR_FORK_FAILURE;
355485
goto end;
356486
}
487+
488+
#if defined(PINO)
489+
if (output == NULL) {
490+
output = input;
491+
}
492+
493+
cur = basename(output);
494+
495+
if (strcmp(cur, LIBCXX_SHARED) == 0) {
496+
if (verbose) {
497+
fprintf(stderr, "- Removing '%s'\n", output);
498+
}
499+
remove_file(output);
500+
}
501+
#endif
357502
}
358503

359504
end:;

tools/gcc-wrapper/errors.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const char* obggcc_strerror(const int err) {
99
return "Memory allocation failed";
1010
case ERR_UNKNOWN_COMPILER:
1111
return "Unrecognized or unsupported C/C++ compiler";
12+
case ERR_UNKNOWN_BINUTILS_WRAPPER:
13+
return "Unrecognized or unsupported binutils wrapper";
1214
case ERR_GET_APP_FILENAME_FAILURE:
1315
return "Failed to retrieve application filename";
1416
case ERR_EXECVE_FAILURE:

tools/gcc-wrapper/errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define ERR_BINFMT_GUESS_FAILURE -10
1515
#define ERR_FORK_FAILURE -11
1616
#define ERR_EXECVE_SUBPROCESS_FAILURE -12
17+
#define ERR_UNKNOWN_BINUTILS_WRAPPER -13
1718

1819
const char* obggcc_strerror(const int err);
1920

0 commit comments

Comments
 (0)