diff options
author | Serge Guelton <sguelton@redhat.com> | 2019-08-21 07:54:42 +0000 |
---|---|---|
committer | Serge Guelton <sguelton@redhat.com> | 2019-08-21 07:54:42 +0000 |
commit | d1262a6e91d5c62120d7f5783821f12e81b079dd (patch) | |
tree | 5e3391bba887f90ce45afa3ed048318beeffd328 | |
parent | 8a42af7b17cf40adab5760a43d1afb819a820a75 (diff) | |
download | bcm5719-llvm-d1262a6e91d5c62120d7f5783821f12e81b079dd.tar.gz bcm5719-llvm-d1262a6e91d5c62120d7f5783821f12e81b079dd.zip |
Be explicit about Windows coff name trailing character policy
It's okay to *not* copy the trailing zero of a windows section/symbol name.
This is compatible with strncpy behavior but gcc doesn't know that and
throws an invalid warning. Encode this behavior in a proper function.
Differential Revision: https://reviews.llvm.org/D66420
llvm-svn: 369501
-rw-r--r-- | llvm/lib/Object/WindowsResource.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/llvm/lib/Object/WindowsResource.cpp b/llvm/lib/Object/WindowsResource.cpp index d76e1231684..762e8bc81d8 100644 --- a/llvm/lib/Object/WindowsResource.cpp +++ b/llvm/lib/Object/WindowsResource.cpp @@ -515,6 +515,14 @@ WindowsResourceCOFFWriter::write(uint32_t TimeDateStamp) { return std::move(OutputBuffer); } +// According to COFF specification, if the Src has a size equal to Dest, +// it's okay to *not* copy the trailing zero. +static void coffnamecpy(char (&Dest)[COFF::NameSize], StringRef Src) { + assert(Src.size() <= COFF::NameSize && + "Src is not larger than COFF::NameSize"); + strncpy(Dest, Src.data(), (size_t)COFF::NameSize); +} + void WindowsResourceCOFFWriter::writeCOFFHeader(uint32_t TimeDateStamp) { // Write the COFF header. auto *Header = reinterpret_cast<coff_file_header *>(BufferStart); @@ -534,7 +542,7 @@ void WindowsResourceCOFFWriter::writeFirstSectionHeader() { CurrentOffset += sizeof(coff_file_header); auto *SectionOneHeader = reinterpret_cast<coff_section *>(BufferStart + CurrentOffset); - strncpy(SectionOneHeader->Name, ".rsrc$01", (size_t)COFF::NameSize); + coffnamecpy(SectionOneHeader->Name, ".rsrc$01"); SectionOneHeader->VirtualSize = 0; SectionOneHeader->VirtualAddress = 0; SectionOneHeader->SizeOfRawData = SectionOneSize; @@ -552,7 +560,7 @@ void WindowsResourceCOFFWriter::writeSecondSectionHeader() { CurrentOffset += sizeof(coff_section); auto *SectionTwoHeader = reinterpret_cast<coff_section *>(BufferStart + CurrentOffset); - strncpy(SectionTwoHeader->Name, ".rsrc$02", (size_t)COFF::NameSize); + coffnamecpy(SectionTwoHeader->Name, ".rsrc$02"); SectionTwoHeader->VirtualSize = 0; SectionTwoHeader->VirtualAddress = 0; SectionTwoHeader->SizeOfRawData = SectionTwoSize; @@ -590,7 +598,7 @@ void WindowsResourceCOFFWriter::writeSymbolTable() { // Now write the symbol table. // First, the feat symbol. auto *Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset); - strncpy(Symbol->Name.ShortName, "@feat.00", (size_t)COFF::NameSize); + coffnamecpy(Symbol->Name.ShortName, "@feat.00"); Symbol->Value = 0x11; Symbol->SectionNumber = 0xffff; Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL; @@ -600,7 +608,7 @@ void WindowsResourceCOFFWriter::writeSymbolTable() { // Now write the .rsrc1 symbol + aux. Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset); - strncpy(Symbol->Name.ShortName, ".rsrc$01", (size_t)COFF::NameSize); + coffnamecpy(Symbol->Name.ShortName, ".rsrc$01"); Symbol->Value = 0; Symbol->SectionNumber = 1; Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL; @@ -619,7 +627,7 @@ void WindowsResourceCOFFWriter::writeSymbolTable() { // Now write the .rsrc2 symbol + aux. Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset); - strncpy(Symbol->Name.ShortName, ".rsrc$02", (size_t)COFF::NameSize); + coffnamecpy(Symbol->Name.ShortName, ".rsrc$02"); Symbol->Value = 0; Symbol->SectionNumber = 2; Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL; @@ -640,7 +648,7 @@ void WindowsResourceCOFFWriter::writeSymbolTable() { for (unsigned i = 0; i < Data.size(); i++) { auto RelocationName = formatv("$R{0:X-6}", i & 0xffffff).sstr<COFF::NameSize>(); Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset); - memcpy(Symbol->Name.ShortName, RelocationName.data(), (size_t) COFF::NameSize); + coffnamecpy(Symbol->Name.ShortName, RelocationName); Symbol->Value = DataOffsets[i]; Symbol->SectionNumber = 2; Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL; |