This is an archive of the discontinued LLVM Phabricator instance.

lld: unquote possibly quoted `EXTERN("symbol")` entry in linker script
ClosedPublic

Authored by luciang on Feb 8 2019, 4:39 PM.

Details

Summary

gold accepts quoted strings:

gold/yyscript.y
252┊ file_cmd:
253┊           EXTERN '(' extern_name_list ')'

312┊ /* A list of external undefined symbols.  We put the lexer into
313┊    expression mode so that commas separate names; this is what the GNU
314┊    linker does.  */
315┊
316┊ extern_name_list:
317┊             { script_push_lex_into_expression_mode(closure); }
318┊           extern_name_list_body
319┊             { script_pop_lex_mode(closure); }
320┊         ;
321┊
322┊ extern_name_list_body:
323┊           string
324┊             { script_add_extern(closure, $1.value, $1.length); }
325┊         | extern_name_list_body string
326┊             { script_add_extern(closure, $2.value, $2.length); }
327┊         | extern_name_list_body ',' string
328┊             { script_add_extern(closure, $3.value, $3.length); }
329┊         ;
330┊

1123┊ /* A string can be either a STRING or a QUOTED_STRING.  Almost all the
1124┊    time we don't care, and we use this rule.  */
1125┊ string:
1126┊           STRING
1127┊             { $$ = $1; }
1128┊         | QUOTED_STRING
1129┊             { $$ = $1; }
1130┊         ;

binutils requires quoted strings for some kinds of symbols, e.g.:

  • it accepts quoted symbols with @ in name:
$ echo 'EXTERN("__libc_start_main@@GLIBC_2.2.5")' > a.script
$ g++ a.script
/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
  • but rejects them if unquoted:
$ echo 'EXTERN(__libc_start_main@@GLIBC_2.2.5)' > a.script
$ g++ a.script
a.script: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status

To maintain compatibility with existing linker scripts support quoted strings in lld as well.

Event Timeline

luciang created this revision.Feb 8 2019, 4:39 PM
ruiu accepted this revision.Feb 8 2019, 4:48 PM

LGTM

This revision is now accepted and ready to land.Feb 8 2019, 4:48 PM
luciang updated this revision to Diff 186081.Feb 8 2019, 5:22 PM

Update test/ELF/undefined-opt.s to verify quoted symbol correctly unquoted

smeenai added a subscriber: smeenai.Feb 8 2019, 7:19 PM

Do you need someone to commit this for you?

Yes, I need help committing this.
Thank you very much! :)

I'll give @ruiu a bit to commit this, since he reviewed it, otherwise I can take care of that for you.

This revision was automatically updated to reflect the committed changes.