<div dir="ltr">D'oh! I see you got this in r<span style="font-family:arial,sans-serif;font-size:13px">217491. Sorry for the noise! (can you tell I read my mail from oldest to most recent?)</span></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 10, 2014 at 12:08 PM, Sean Silva <span dir="ltr"><<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span class=""><span style="font-family:arial,sans-serif;font-size:13px">+    char *endPtr;</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">+    uint64_t baseAddress = strtoull(imageBase->getValue()</span><span style="font-family:arial,sans-serif;font-size:13px">, &endPtr, 16);</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">+    if (*endPtr != 0) {</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">+      diagnostics << "error: image_base expects a hex number\n";</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">+      return false;</span><br><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div></span><div><span style="font-family:arial,sans-serif;font-size:13px">May I suggest StringRef::getAsInteger to avoid this raw C-style stuff?</span></div><span class="HOEnZb"><font color="#888888"><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">-- Sean Silva</span></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 10, 2014 at 3:39 AM, Tim Northover <span dir="ltr"><<a href="mailto:tnorthover@apple.com" target="_blank">tnorthover@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: tnorthover<br>
Date: Wed Sep 10 05:39:57 2014<br>
New Revision: 217486<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=217486&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=217486&view=rev</a><br>
Log:<br>
[mach-o]: implement -image_base option on Darwin.<br>
<br>
As suggested by Nick, this will make __unwind_info implementation more natural,<br>
and it'd have to be done at some point anyway.<br>
<br>
Added:<br>
    lld/trunk/test/mach-o/image-base.yaml<br>
Modified:<br>
    lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h<br>
    lld/trunk/lib/Driver/DarwinLdDriver.cpp<br>
    lld/trunk/lib/Driver/DarwinLdOptions.td<br>
    lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp<br>
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp<br>
<br>
Modified: lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=217486&r1=217485&r2=217486&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=217486&r1=217485&r2=217486&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)<br>
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Wed Sep 10 05:39:57 2014<br>
@@ -108,6 +108,9 @@ public:<br>
   bool PIE() const { return _pie; }<br>
   void setPIE(bool pie) { _pie = pie; }<br>
<br>
+  uint64_t baseAddress() const { return _baseAddress; }<br>
+  void setBaseAddress(uint64_t baseAddress) { _baseAddress = baseAddress; }<br>
+<br>
   /// \brief Checks whether a given path on the filesystem exists.<br>
   ///<br>
   /// When running in -test_file_usage mode, this method consults an<br>
@@ -275,6 +278,7 @@ private:<br>
   uint32_t _osMinVersion;<br>
   uint64_t _pageZeroSize;<br>
   uint64_t _pageSize;<br>
+  uint64_t _baseAddress;<br>
   uint32_t _compatibilityVersion;<br>
   uint32_t _currentVersion;<br>
   StringRef _installName;<br>
<br>
Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=217486&r1=217485&r2=217486&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=217486&r1=217485&r2=217486&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)<br>
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Wed Sep 10 05:39:57 2014<br>
@@ -33,6 +33,7 @@<br>
 #include "llvm/Support/Signals.h"<br>
<br>
 #include <algorithm><br>
+#include <cstdlib><br>
<br>
 using namespace lld;<br>
<br>
@@ -312,6 +313,20 @@ bool DarwinLdDriver::parse(int argc, con<br>
   else<br>
     ctx.setOutputPath("a.out");<br>
<br>
+  if (llvm::opt::Arg *imageBase = parsedArgs->getLastArg(OPT_image_base)) {<br>
+    char *endPtr;<br>
+    uint64_t baseAddress = strtoull(imageBase->getValue(), &endPtr, 16);<br>
+    if (*endPtr != 0) {<br>
+      diagnostics << "error: image_base expects a hex number\n";<br>
+      return false;<br>
+    } else if  (baseAddress < ctx.pageZeroSize()) {<br>
+      diagnostics << "error: image_base overlaps with __PAGEZERO\n";<br>
+      return false;<br>
+    }<br>
+<br>
+    ctx.setBaseAddress(baseAddress);<br>
+  }<br>
+<br>
   // Handle -dead_strip<br>
   if (parsedArgs->getLastArg(OPT_dead_strip))<br>
     ctx.setDeadStripping(true);<br>
<br>
Modified: lld/trunk/lib/Driver/DarwinLdOptions.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdOptions.td?rev=217486&r1=217485&r2=217486&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdOptions.td?rev=217486&r1=217485&r2=217486&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/Driver/DarwinLdOptions.td (original)<br>
+++ lld/trunk/lib/Driver/DarwinLdOptions.td Wed Sep 10 05:39:57 2014<br>
@@ -142,6 +142,7 @@ def arch : Separate<["-"], "arch">,<br>
 def sectalign : MultiArg<["-"], "sectalign", 3>,<br>
      MetaVarName<"<segname> <sectname> <alignment>">,<br>
      HelpText<"alignment for segment/section">;<br>
+def image_base : Separate<["-"], "image_base">;<br>
 def t : Flag<["-"], "t">,<br>
      HelpText<"Print the names of the input files as ld processes them">;<br>
 def v : Flag<["-"], "v">,<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=217486&r1=217485&r2=217486&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=217486&r1=217485&r2=217486&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp Wed Sep 10 05:39:57 2014<br>
@@ -133,11 +133,10 @@ bool MachOLinkingContext::isThinObjectFi<br>
<br>
 MachOLinkingContext::MachOLinkingContext()<br>
     : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false),<br>
-      _doNothing(false), _pie(false),<br>
-      _arch(arch_unknown), _os(OS::macOSX), _osMinVersion(0),<br>
-      _pageZeroSize(0), _pageSize(4096), _compatibilityVersion(0),<br>
-      _currentVersion(0), _deadStrippableDylib(false), _printAtoms(false),<br>
-      _testingFileUsage(false), _keepPrivateExterns(false),<br>
+      _doNothing(false), _pie(false), _arch(arch_unknown), _os(OS::macOSX),<br>
+      _osMinVersion(0), _pageZeroSize(0), _pageSize(4096), _baseAddress(0),<br>
+      _compatibilityVersion(0), _currentVersion(0), _deadStrippableDylib(false),<br>
+      _printAtoms(false), _testingFileUsage(false), _keepPrivateExterns(false),<br>
       _archHandler(nullptr), _exportMode(ExportMode::globals) {}<br>
<br>
 MachOLinkingContext::~MachOLinkingContext() {}<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=217486&r1=217485&r2=217486&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=217486&r1=217485&r2=217486&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Wed Sep 10 05:39:57 2014<br>
@@ -95,8 +95,8 @@ SegmentInfo::SegmentInfo(StringRef n)<br>
<br>
 class Util {<br>
 public:<br>
-  Util(const MachOLinkingContext &ctxt) : _context(ctxt),<br>
-    _archHandler(ctxt.archHandler()), _entryAtom(nullptr) {}<br>
+  Util(const MachOLinkingContext &ctxt)<br>
+      : _context(ctxt), _archHandler(ctxt.archHandler()), _entryAtom(nullptr) {}<br>
<br>
   void      assignAtomsToSections(const lld::File &atomFile);<br>
   void      organizeSections();<br>
@@ -468,16 +468,19 @@ void Util::layoutSectionsInTextSegment(s<br>
<br>
 void Util::assignAddressesToSections(const NormalizedFile &file) {<br>
   size_t hlcSize = headerAndLoadCommandsSize(file);<br>
-  uint64_t address = 0;  // FIXME<br>
+  uint64_t address = 0;<br>
   if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {<br>
     for (SegmentInfo *seg : _segmentInfos) {<br>
       if (seg->name.equals("__PAGEZERO")) {<br>
         seg->size = _context.pageZeroSize();<br>
         address += seg->size;<br>
       }<br>
-      else if (seg->name.equals("__TEXT"))<br>
+      else if (seg->name.equals("__TEXT")) {<br>
+        // _context.baseAddress()  == 0 implies it was either unspecified or<br>
+        // pageZeroSize is also 0. In either case resetting address is safe.<br>
+        address = _context.baseAddress() ? _context.baseAddress() : address;<br>
         layoutSectionsInTextSegment(hlcSize, seg, address);<br>
-      else<br>
+      } else<br>
         layoutSectionsInSegment(seg, address);<br>
<br>
       address = llvm::RoundUpToAlignment(address, _context.pageSize());<br>
<br>
Added: lld/trunk/test/mach-o/image-base.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/image-base.yaml?rev=217486&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/image-base.yaml?rev=217486&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/mach-o/image-base.yaml (added)<br>
+++ lld/trunk/test/mach-o/image-base.yaml Wed Sep 10 05:39:57 2014<br>
@@ -0,0 +1,14 @@<br>
+# RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 %s -o %t -image_base 31415926530 %p/Inputs/libSystem.yaml<br>
+# RUN: macho-dump %t | FileCheck %s<br>
+<br>
+--- !native<br>
+defined-atoms:<br>
+   - name:            _main<br>
+     scope:           global<br>
+     content:         []<br>
+<br>
+  # Unfortunately, llvm-objdump and llvm-readobj are too generic and don't give<br>
+  # us easy access to the MachO segment model, so we have to check the uglier<br>
+  # macho-dump output.<br>
+# CHECK: 'segment_name', '__TEXT<br>
+# CHECK-NEXT: 'vm_addr', 3384796144944<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>