<div dir="ltr"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  // DOS Stub. DOS stub is data located at the beginning of PE/COFF file.</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">
<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  // Windows loader do not really care about DOS stub contents, but it's usually</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">
<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  // a small DOS program that prints out a message "This program requires</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">
<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  // Microsoft Windows." This feature was somewhat useful before Windows 95.</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">
<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  std::vector<uint8_t> _dosStub;</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">+  static const std::vector<uint8_t> _defaultDosStub;</span><br>
<div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">Why on earth is this using a std::vector? (which adds a static initializer!) Just use ArrayRef or even a const char array.</span></div>
<div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">Also, do we really need to support this feature? Who uses /stub anymore?</span></div>
<div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">-- Sean Silva</span></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Thu, Nov 14, 2013 at 6:54 PM, Rui Ueyama <span dir="ltr"><<a href="mailto:ruiu@google.com" target="_blank">ruiu@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: ruiu<br>
Date: Thu Nov 14 17:54:24 2013<br>
New Revision: 194754<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=194754&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=194754&view=rev</a><br>
Log:<br>
Move DOS stub data to PECOFFLinkingContext for /stub option.<br>
<br>
Modified:<br>
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h<br>
    lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp<br>
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp<br>
<br>
Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=194754&r1=194753&r2=194754&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=194754&r1=194753&r2=194754&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)<br>
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Thu Nov 14 17:54:24 2013<br>
@@ -43,7 +43,7 @@ public:<br>
         _terminalServerAware(true), _dynamicBaseEnabled(true),<br>
         _createManifest(true), _embedManifest(false), _manifestId(1),<br>
         _manifestLevel("'asInvoker'"), _manifestUiAccess("'false'"),<br>
-        _imageType(ImageType::IMAGE_EXE) {<br>
+        _imageType(ImageType::IMAGE_EXE), _dosStub(_defaultDosStub) {<br>
     setDeadStripping(true);<br>
   }<br>
<br>
@@ -230,6 +230,10 @@ public:<br>
     return it == _sectionAttributeMask.end() ? 0 : it->second;<br>
   }<br>
<br>
+  const std::vector<uint8_t> &getDosStub() const {<br>
+    return _dosStub;<br>
+  }<br>
+<br>
   StringRef allocateString(StringRef ref) const {<br>
     char *x = _allocator.Allocate<char>(ref.size() + 1);<br>
     memcpy(x, ref.data(), ref.size());<br>
@@ -311,6 +315,13 @@ private:<br>
<br>
   // List of files that will be removed on destruction.<br>
   std::vector<std::unique_ptr<llvm::FileRemover> > _tempFiles;<br>
+<br>
+  // DOS Stub. DOS stub is data located at the beginning of PE/COFF file.<br>
+  // Windows loader do not really care about DOS stub contents, but it's usually<br>
+  // a small DOS program that prints out a message "This program requires<br>
+  // Microsoft Windows." This feature was somewhat useful before Windows 95.<br>
+  std::vector<uint8_t> _dosStub;<br>
+  static const std::vector<uint8_t> _defaultDosStub;<br>
 };<br>
<br>
 } // end namespace lld<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=194754&r1=194753&r2=194754&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=194754&r1=194753&r2=194754&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Thu Nov 14 17:54:24 2013<br>
@@ -29,6 +29,13 @@<br>
<br>
 namespace lld {<br>
<br>
+namespace {<br>
+uint8_t DefaultDosStub[128] = {'M', 'Z'};<br>
+};<br>
+<br>
+const std::vector<uint8_t> PECOFFLinkingContext::_defaultDosStub(<br>
+    DefaultDosStub, DefaultDosStub + sizeof(DefaultDosStub));<br>
+<br>
 bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {<br>
   if (_stackReserve < _stackCommit) {<br>
     diagnostics << "Invalid stack size: reserve size must be equal to or "<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=194754&r1=194753&r2=194754&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=194754&r1=194753&r2=194754&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Nov 14 17:54:24 2013<br>
@@ -106,29 +106,19 @@ public:<br>
 /// of PE/COFF files.<br>
 class DOSStubChunk : public HeaderChunk {<br>
 public:<br>
-  DOSStubChunk() : HeaderChunk() {<br>
-    // Make the DOS stub occupy the first 128 bytes of an exe. Technically<br>
-    // this can be as small as 64 bytes, but GNU binutil's objdump cannot<br>
-    // parse such irregular header.<br>
-    _size = 128;<br>
-<br>
-    // A DOS stub is usually a small valid DOS program that prints out a message<br>
-    // "This program requires Microsoft Windows" to help user who accidentally<br>
-    // run a Windows executable on DOS. That's not a technical requirement, so<br>
-    // we don't bother to emit such code, at least for now. We simply fill the<br>
-    // DOS stub with null bytes.<br>
-    std::memset(&_dosHeader, 0, sizeof(_dosHeader));<br>
-<br>
-    _dosHeader.Magic = 'M' | ('Z' << 8);<br>
-    _dosHeader.AddressOfNewExeHeader = _size;<br>
+  DOSStubChunk(const PECOFFLinkingContext &ctx)<br>
+      : HeaderChunk(), _dosStub(ctx.getDosStub()) {<br>
+    auto *header = reinterpret_cast<llvm::object::dos_header *>(&_dosStub[0]);<br>
+    header->AddressOfNewExeHeader = _dosStub.size();<br>
+    _size = _dosStub.size();<br>
   }<br>
<br>
   virtual void write(uint8_t *fileBuffer) {<br>
-    std::memcpy(fileBuffer, &_dosHeader, sizeof(_dosHeader));<br>
+    std::memcpy(fileBuffer, &_dosStub[0], _dosStub.size());<br>
   }<br>
<br>
 private:<br>
-  llvm::object::dos_header _dosHeader;<br>
+  std::vector<uint8_t> _dosStub;<br>
 };<br>
<br>
 /// A PEHeaderChunk represents PE header including COFF header.<br>
@@ -810,7 +800,7 @@ public:<br>
   // Create all chunks that consist of the output file.<br>
   void build(const File &linkedFile) {<br>
     // Create file chunks and add them to the list.<br>
-    auto *dosStub = new DOSStubChunk();<br>
+    auto *dosStub = new DOSStubChunk(_PECOFFLinkingContext);<br>
     auto *peHeader = new PEHeaderChunk(_PECOFFLinkingContext);<br>
     auto *dataDirectory = new DataDirectoryChunk(linkedFile);<br>
     auto *sectionTable = new SectionHeaderTableChunk();<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">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>