[PATCH] [lld][PECOFF] Fix section alignment bug in ReaderCOFF

Ron Ofir ron.ofir at gmail.com
Fri Sep 6 11:30:53 PDT 2013


Hi rui314,

This fixes a small bug in ReaderCOFF getAlignment function.

The original behaviour was to take what ever value is stored in bits 20:24 of the characteristics field and use that as the exponent, while this value should actually be decreased by 1 to get the intended exponent (ALIGN_1BYTES is stored as 1 instead of 0, ALIGN_2BYTES is stored as 2 instead of 1 and ALIGN_8192BYTES is stored as 14 instead of 13).

http://llvm-reviews.chandlerc.com/D1620

Files:
  lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
  test/pecoff/lib.test
  test/pecoff/multi.test

Index: lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
===================================================================
--- lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
+++ lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
@@ -87,8 +87,10 @@
 DefinedAtom::Alignment getAlignment(const coff_section *section) {
   if (section->Characteristics & llvm::COFF::IMAGE_SCN_TYPE_NO_PAD)
     return DefinedAtom::Alignment(0);
-  // Bit [20:24] contains section alignment information.
-  int powerOf2 = (section->Characteristics >> 20) & 0xf;
+  // Bit [20:24] contains section alignment information. We need to decrease
+  // the value stored by 1 in order to get the real exponent (e.g, ALIGN_1BYTE
+  // is 0x00100000, but the exponent should be 0)
+  int powerOf2 = ((section->Characteristics >> 20) & 0xf) - 1;
   return DefinedAtom::Alignment(powerOf2);
 }
 
Index: test/pecoff/lib.test
===================================================================
--- test/pecoff/lib.test
+++ test/pecoff/lib.test
@@ -7,6 +7,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000: a1 08 20 40 00      movl 4202504, %eax
+CHECK:     1000: a1 04 20 40 00      movl 4202500, %eax
 CHECK:     1005: 03 05 00 20 40 00   addl 4202496, %eax
 CHECK:     100b: c3                  ret
Index: test/pecoff/multi.test
===================================================================
--- test/pecoff/multi.test
+++ test/pecoff/multi.test
@@ -9,6 +9,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000: a1 08 20 40 00      movl 4202504, %eax
+CHECK:     1000: a1 04 20 40 00      movl 4202500, %eax
 CHECK:     1005: 03 05 00 20 40 00   addl 4202496, %eax
 CHECK:     100b: c3                  ret
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1620.1.patch
Type: text/x-patch
Size: 1699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130906/00f2af3f/attachment.bin>


More information about the llvm-commits mailing list