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;
2023static const char HYPHEN [] = "-" ;
2124static 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+
190299int 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 :;
0 commit comments