This is an alternative to D38238. When parsing linker scripts, LLD starts with a '.' value of 0, regardless of the internal default image base for the target, and regardless of switches such as --image-base. It seems reasonable to use a different image base value when using linker scripts and --image-base is specified, since otherwise the switch has no effect. However, the default image base should not be used when processing linker scripts, because this will change the behaviour for existing linker script users, and potentially result in invalid output being produced, as a subsequent assignment to dot could move the location counter backwards (see the comments in D38238 for examples).
Details
Diff Detail
Event Timeline
ELF/Config.h | ||
---|---|---|
170 | I think proper place for this would be ScriptConfiguration, |
ELF/Driver.cpp | ||
---|---|---|
918–919 | This and other functions in this file are intended to be "pure" functions, that don't have any side-effects. They are "getters" and are not expected to do anything other than returning values. So please do not set a value to a Config member from a "get" function. |
Address review comment.
@grimar - I don't think the new variable should be in ScriptConfiguration. From what I can tell, ScriptConfiguration is intended for settings controlled by the linker script, which this is not. This is controlled by the command-line.
ELF/Config.h | ||
---|---|---|
169–170 | So, now you have two variables for the image base. Their valid combinations are:
Other combinations are all invalid. That means you are effectively using InitiaDot as a boolean variable. It doesn't have to be an integer, and to convey the intention how it is used, it should be a boolean. I'd make these changes:
|
Addressed review comments.
I'm not sure where to put the new getImageBase function, because it is needed in SyntheticSections.cpp as well as LinkerScript.cpp. To me it feels like it really belongs as a member of the Configuration class, but that is currently made up entirely of variables, and no methods, so I've put it in the LinkerScript class. I'm open to other suggestions.
ELF/Driver.cpp | ||
---|---|---|
924 | It is common to return None; |
ELF/LinkerScript.cpp | ||
---|---|---|
349 | I wonder if you really have to call this function twice. Can you initialize Dot only once in LinkerScript's ctor? | |
355 | This function does not use any LinkerScript's private/protected members. It is a good practice to not make such function a member function of some class. They can just be a non-member function. By doing that, it is obvious that the function doesn't access any private members. That said, you can move this function to Target, and by doing that, you can make Target::DefaultImageBase a protected member. Can you do that? |
ELF/LinkerScript.cpp | ||
---|---|---|
349 | No, because the value is not final until after command-line processing, whereas the LinkerScript instance is constructed during program initialisation. We also need to reset Dot every time we call assignAddresses, because it might be called multiple times. However, having looked closer it does not need setting in processCommands (it's never updated, and should never be referenced, although it currently could be in SUBALIGN expressions - this is a bug, these expressions should only accept constants, and the current behaviour results in an assertion failure if it is used). This allows me to move the function inline into assignAddresses. |
I think proper place for this would be ScriptConfiguration,
though please wait for Rui's opinion.