Wrong section alignments on linux ELF cause linking problems

Wrong section alignments on linux ELF cause linking problems

We wanted to use the LLVM LLD linker, but it complains about an incorrect section alignment emitted by ICPC. Here is my corresponding bug report to LLVM: https://bugs.llvm.org/show_bug.cgi?id=35854 . It turns out the bug apparently is within the Intel Compiler. Quoting my bugreport:

 

"

I would like to raise this as a feature request for compatibility with GNU LD: LD supports unaligned sections in object files, which are created at least by the Intel ICC compiler and prevents us to adopt LLD in our build. 

   ld: error: .../_moc_mainwindow.o: unaligned data

Further investigation shows

   ~/usr/bin/llvm-readelf -relocations _moc_mainwindow.o

    Relocation section '.rel.data' at offset 0x2546f contains 1 entries:
     Offset     Info    Type                Sym. Value  Symbol's Name

    Error reading file: unaligned data
    .

This works with GNU LD and GNU GOLD

"

 

ICC Version:

icpc (ICC) 15.0.3 20150407

Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

 

Please provide us a test case to investigate.

Regards,

Viet

Testcase and Bug:

:~/cpp$ cat main.cpp
int main() { }

:~/cpp$ icpc -c main.cpp

:~/cpp$ llvm-readelf -relocations main.o 

Relocation section '.rel.text' at offset 0x189 contains 1 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name

Error reading file: unaligned data
.

(notice the unaligned offset 0x189). People from LLVM have provided this technical comment:

"Essentially, all data structures in ELF should be aligned to their natural boundaries both in memory and file: Quote from http://refspecs.linuxbase.org/elf/elf.pdf p.16 "All data structures that the object file format defines follow the "natural" size and alignment guidelines for the relevant class. If necessary, data structures contain explicit padding to ensure 4-byte alignment for 4-byte objects, to force structure sizes to a multiple of 4, and so on. Data also have suitable alignment from the beginning of the file. Thus, for example, a structure containing an Elf32_Addr member will be aligned on a 4-byte boundary within the file."

 

 

icpc (ICC) 15.0.3 20150407

You probably use too old compiler.

I tried 15.0.5  Build 20150805 and llvm-readelf built from trunk 6.0 and got the following output:

$ cat main.cpp
int main() {}

$ icpc -V -c main.cpp
Intel(R) C++ Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0 Build 20150805
Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

$ llvm-readelf -relocations main.o

Relocation section '.rela.text' at offset 0x284 contains 1 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000000017  0000000900000002 R_X86_64_PC32          0000000000000000 __intel_new_feature_proc_init - 4

Relocation section '.rela.eh_frame' at offset 0x29c contains 2 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000000011  0000000a00000001 R_X86_64_64            0000000000000000 __gxx_personality_v0 + 0
0000000000000028  0000000400000001 R_X86_64_64            0000000000000000 .text + 0

With the recent ICC 18.0 it also works fine.

Leave a Comment

Please sign in to add a comment. Not a member? Join today