[lld] r194754 - Move DOS stub data to PECOFFLinkingContext for /stub option.

Rui Ueyama ruiu at google.com
Thu Nov 14 21:16:09 PST 2013


On Thu, Nov 14, 2013 at 8:54 PM, Sean Silva <silvas at purdue.edu> wrote:

> +  // DOS Stub. DOS stub is data located at the beginning of PE/COFF file.
> +  // Windows loader do not really care about DOS stub contents, but it's
> usually
> +  // a small DOS program that prints out a message "This program requires
> +  // Microsoft Windows." This feature was somewhat useful before Windows
> 95.
> +  std::vector<uint8_t> _dosStub;
> +  static const std::vector<uint8_t> _defaultDosStub;
>
> Why on earth is this using a std::vector? (which adds a static
> initializer!) Just use ArrayRef or even a const char array.
>

I wanted to make PECOFFLinkingContext to have the ownership of the vector,
so I just made it a utf8 vector. But yes, std::vector may be bad and it's
almost always a waste of memory because the contents are almost always the
same. const char array wouldn't work because it's size may vary. I'll make
it an ArrayRef.

Also, do we really need to support this feature? Who uses /stub anymore?
>

It's very rare, if any. However, I'd think it's still worth adding for
feature-completeness. It's also easy to implement.

-- Sean Silva
>
>
> On Thu, Nov 14, 2013 at 6:54 PM, Rui Ueyama <ruiu at google.com> wrote:
>
>> Author: ruiu
>> Date: Thu Nov 14 17:54:24 2013
>> New Revision: 194754
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=194754&view=rev
>> Log:
>> Move DOS stub data to PECOFFLinkingContext for /stub option.
>>
>> Modified:
>>     lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
>>     lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
>>     lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
>>
>> Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=194754&r1=194753&r2=194754&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
>> +++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Thu Nov 14
>> 17:54:24 2013
>> @@ -43,7 +43,7 @@ public:
>>          _terminalServerAware(true), _dynamicBaseEnabled(true),
>>          _createManifest(true), _embedManifest(false), _manifestId(1),
>>          _manifestLevel("'asInvoker'"), _manifestUiAccess("'false'"),
>> -        _imageType(ImageType::IMAGE_EXE) {
>> +        _imageType(ImageType::IMAGE_EXE), _dosStub(_defaultDosStub) {
>>      setDeadStripping(true);
>>    }
>>
>> @@ -230,6 +230,10 @@ public:
>>      return it == _sectionAttributeMask.end() ? 0 : it->second;
>>    }
>>
>> +  const std::vector<uint8_t> &getDosStub() const {
>> +    return _dosStub;
>> +  }
>> +
>>    StringRef allocateString(StringRef ref) const {
>>      char *x = _allocator.Allocate<char>(ref.size() + 1);
>>      memcpy(x, ref.data(), ref.size());
>> @@ -311,6 +315,13 @@ private:
>>
>>    // List of files that will be removed on destruction.
>>    std::vector<std::unique_ptr<llvm::FileRemover> > _tempFiles;
>> +
>> +  // DOS Stub. DOS stub is data located at the beginning of PE/COFF file.
>> +  // Windows loader do not really care about DOS stub contents, but it's
>> usually
>> +  // a small DOS program that prints out a message "This program requires
>> +  // Microsoft Windows." This feature was somewhat useful before Windows
>> 95.
>> +  std::vector<uint8_t> _dosStub;
>> +  static const std::vector<uint8_t> _defaultDosStub;
>>  };
>>
>>  } // end namespace lld
>>
>> Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=194754&r1=194753&r2=194754&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)
>> +++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Thu Nov 14
>> 17:54:24 2013
>> @@ -29,6 +29,13 @@
>>
>>  namespace lld {
>>
>> +namespace {
>> +uint8_t DefaultDosStub[128] = {'M', 'Z'};
>> +};
>> +
>> +const std::vector<uint8_t> PECOFFLinkingContext::_defaultDosStub(
>> +    DefaultDosStub, DefaultDosStub + sizeof(DefaultDosStub));
>> +
>>  bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
>>    if (_stackReserve < _stackCommit) {
>>      diagnostics << "Invalid stack size: reserve size must be equal to or
>> "
>>
>> Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=194754&r1=194753&r2=194754&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
>> +++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Nov 14
>> 17:54:24 2013
>> @@ -106,29 +106,19 @@ public:
>>  /// of PE/COFF files.
>>  class DOSStubChunk : public HeaderChunk {
>>  public:
>> -  DOSStubChunk() : HeaderChunk() {
>> -    // Make the DOS stub occupy the first 128 bytes of an exe.
>> Technically
>> -    // this can be as small as 64 bytes, but GNU binutil's objdump cannot
>> -    // parse such irregular header.
>> -    _size = 128;
>> -
>> -    // A DOS stub is usually a small valid DOS program that prints out a
>> message
>> -    // "This program requires Microsoft Windows" to help user who
>> accidentally
>> -    // run a Windows executable on DOS. That's not a technical
>> requirement, so
>> -    // we don't bother to emit such code, at least for now. We simply
>> fill the
>> -    // DOS stub with null bytes.
>> -    std::memset(&_dosHeader, 0, sizeof(_dosHeader));
>> -
>> -    _dosHeader.Magic = 'M' | ('Z' << 8);
>> -    _dosHeader.AddressOfNewExeHeader = _size;
>> +  DOSStubChunk(const PECOFFLinkingContext &ctx)
>> +      : HeaderChunk(), _dosStub(ctx.getDosStub()) {
>> +    auto *header = reinterpret_cast<llvm::object::dos_header
>> *>(&_dosStub[0]);
>> +    header->AddressOfNewExeHeader = _dosStub.size();
>> +    _size = _dosStub.size();
>>    }
>>
>>    virtual void write(uint8_t *fileBuffer) {
>> -    std::memcpy(fileBuffer, &_dosHeader, sizeof(_dosHeader));
>> +    std::memcpy(fileBuffer, &_dosStub[0], _dosStub.size());
>>    }
>>
>>  private:
>> -  llvm::object::dos_header _dosHeader;
>> +  std::vector<uint8_t> _dosStub;
>>  };
>>
>>  /// A PEHeaderChunk represents PE header including COFF header.
>> @@ -810,7 +800,7 @@ public:
>>    // Create all chunks that consist of the output file.
>>    void build(const File &linkedFile) {
>>      // Create file chunks and add them to the list.
>> -    auto *dosStub = new DOSStubChunk();
>> +    auto *dosStub = new DOSStubChunk(_PECOFFLinkingContext);
>>      auto *peHeader = new PEHeaderChunk(_PECOFFLinkingContext);
>>      auto *dataDirectory = new DataDirectoryChunk(linkedFile);
>>      auto *sectionTable = new SectionHeaderTableChunk();
>>
>>
>> _______________________________________________
>> 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/20131114/e35f519d/attachment.html>


More information about the llvm-commits mailing list