[lld] r217578 - [mach-o]: support optional "0x" prefix for -image_base

Nick Kledzik kledzik at apple.com
Thu Sep 11 16:48:13 PDT 2014


On Sep 11, 2014, at 4:20 PM, Rui Ueyama <ruiu at google.com> wrote:

> In most languages that I know, if one accepts "0x" as a hex digit prefix, it also accepts "0X". So I'm wondering if it has to compare the prefix case insensitive way. I did not test it though. I'm sorry if it missed the point.
I’d not heard of “0X” as being a valid prefix.  For instance, llvm’s StringRef::getAsUnsignedInteger() does not auto sense “0X” as a base 16 prefix.

ld64 just used strtoull().  The man page says that the string may include the “0x” prefix and the number will be read as base 16. So I would not expect “0X” to work.  But the implementation of strtoull() actually checks for “0x” or “0X”, so ld64 does accept “0X” prefix! 

That said, where ever the darwin linker takes an address or size as an option, it is assumed to be in hex (hey, its for programmers). The 0x is optional (for explicitness).  But this does not map well to llvm’s StringRef::getAsUnsignedInteger().  So, I think the darwin driver needs a utility routine for parsing address options.  And might as well have that utility routine handle “0X” too.  

I’ll work up a patch.

-Nick

> 
> On Thu, Sep 11, 2014 at 3:31 AM, Tim Northover <tnorthover at apple.com> wrote:
> Author: tnorthover
> Date: Thu Sep 11 05:31:46 2014
> New Revision: 217578
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=217578&view=rev
> Log:
> [mach-o]: support optional "0x" prefix for -image_base
> 
> Modified:
>     lld/trunk/lib/Driver/DarwinLdDriver.cpp
>     lld/trunk/test/mach-o/image-base.yaml
> 
> Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=217578&r1=217577&r2=217578&view=diff
> ==============================================================================
> --- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)
> +++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Thu Sep 11 05:31:46 2014
> @@ -313,8 +313,12 @@ bool DarwinLdDriver::parse(int argc, con
>      ctx.setOutputPath("a.out");
> 
>    if (llvm::opt::Arg *imageBase = parsedArgs->getLastArg(OPT_image_base)) {
> +    StringRef baseString = imageBase->getValue();
> +    if (baseString.startswith("0x"))
> +      baseString = baseString.drop_front(2);
> +
>      uint64_t baseAddress;
> -    if (StringRef(imageBase->getValue()).getAsInteger(16, baseAddress)) {
> +    if (baseString.getAsInteger(16, baseAddress)) {
>        diagnostics << "error: image_base expects a hex number\n";
>        return false;
>      } else if (baseAddress < ctx.pageZeroSize()) {
> 
> Modified: lld/trunk/test/mach-o/image-base.yaml
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/image-base.yaml?rev=217578&r1=217577&r2=217578&view=diff
> ==============================================================================
> --- lld/trunk/test/mach-o/image-base.yaml (original)
> +++ lld/trunk/test/mach-o/image-base.yaml Thu Sep 11 05:31:46 2014
> @@ -1,6 +1,6 @@
>  # RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 %s -o %t -image_base 31415926000 %p/Inputs/libSystem.yaml
>  # RUN: macho-dump %t | FileCheck %s
> -# RUN: not lld -flavor darwin -arch x86_64 -image_base 31415926530 %s >/dev/null 2> %t
> +# RUN: not lld -flavor darwin -arch x86_64 -image_base 0x31415926530 %s >/dev/null 2> %t
>  # RUN: FileCheck < %t %s --check-prefix=CHECK-ERROR-MISPAGED
>  # RUN: not lld -flavor darwin -arch x86_64 -image_base 1000 %s >/dev/null 2> %t
>  # RUN: FileCheck < %t %s --check-prefix=CHECK-ERROR-OVERLAP
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140911/1741c4a6/attachment.html>


More information about the llvm-commits mailing list