This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Fix the file look up algorithm used in the linker script GROUP command.
ClosedPublic

Authored by atanasyan on Apr 30 2014, 12:14 PM.

Details

Summary

In general the linker scripts' GROUP command works like a pair of command line options --start-group/--end-group. But there is a difference in the files look up algorithm.

The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so' suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.

The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so' suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured, and the path starts with the / character, and the script being processed is located inside the sysroot, search the path under the sysroot. Otherwise, try to open the path in the current directory. If it is not found, search through library search directories.

https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html

Diff Detail

Event Timeline

atanasyan updated this revision to Diff 8989.Apr 30 2014, 12:14 PM
atanasyan retitled this revision from to [ELF] Fix the file look up algorithm used in the linker script GROUP command..
atanasyan updated this object.
atanasyan edited the test plan for this revision. (Show Details)
atanasyan added reviewers: Bigcheese, shankarke, ruiu.
atanasyan added a subscriber: Unknown Object (MLST).
shankarke added inline comments.Apr 30 2014, 12:48 PM
include/lld/ReaderWriter/ELFLinkingContext.h
176–180

There is also a third thing, to support namespaces. You could refer to -l:mylib.a, the linker would search by not adding a prefix 'lib'.

lib/Driver/GnuLdInputGraph.cpp
76–86

can we just use realpath and compare if the file is a prefix of another ?

95

we should probably reject sysroot being empty during parsing command line options, that way we can get rid of empty() comparison.

atanasyan added inline comments.Apr 30 2014, 1:02 PM
include/lld/ReaderWriter/ELFLinkingContext.h
176–180

I did not plan to support namespaces in this fix. But now I think it is a good idea to cover all cases by this patch. Will implement this soon.

lib/Driver/GnuLdInputGraph.cpp
76–86

I use the llvm::sys::fs::equivalent() to make this code a bit more platform independent. As far as I understand on Windows it is possible to get sysroot = 'C:\work_folder\SysRoot' and path = 'c:\work_folder\sysroot\file_name'. So on Windows we have to use case insensitive string comparison and case sensitive string comparison on Unix.

95

I check that the sysroot is not empty here to recognize a valid case when a user does not provide --sysroot option at all.

ruiu accepted this revision.Apr 30 2014, 1:10 PM
ruiu edited edge metadata.

LGTM

This revision is now accepted and ready to land.Apr 30 2014, 1:10 PM
shankarke accepted this revision.Apr 30 2014, 4:01 PM
shankarke edited edge metadata.
atanasyan closed this revision.May 1 2014, 9:29 AM

Thanks for review.

Closed by commit rL207769 (authored by @atanasyan)

include/lld/ReaderWriter/ELFLinkingContext.h
176–180

Namespace support makes this patch too big. For example, now we cannot correctly parse namespace in a linker script at all. So I will implement this in a separate patch.