<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:NimbusMonL-Regu;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:NimbusRomNo9L-Regu;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:NimbusRomNo9L-ReguItal;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.fontstyle01
        {mso-style-name:fontstyle01;
        font-family:"NimbusMonL-Regu",serif;
        color:black;
        font-weight:normal;
        font-style:normal;}
span.fontstyle21
        {mso-style-name:fontstyle21;
        font-family:"NimbusRomNo9L-Regu",serif;
        color:black;
        font-weight:normal;
        font-style:normal;}
span.fontstyle31
        {mso-style-name:fontstyle31;
        font-family:"NimbusRomNo9L-ReguItal",serif;
        color:black;
        font-weight:normal;
        font-style:italic;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:448279553;
        mso-list-type:hybrid;
        mso-list-template-ids:195200096 67698703 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level3
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
@list l0:level4
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Symbol;}
@list l0:level5
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level6
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
@list l0:level7
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Symbol;}
@list l0:level8
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level9
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
@list l1
        {mso-list-id:1009217898;
        mso-list-type:hybrid;
        mso-list-template-ids:-581513564 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l1:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi Rui,<o:p></o:p></p>
<p class="MsoNormal">I still fail to enable the lld in my Uefi firmware build to replace ld, and I found it is related to the wrong symbol values in the .data section, which are pointed by R_X86_64_64 relocation entries. I need your advices.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">My firmware uses a linker script <a href="https://github.com/tianocore/edk2/blob/master/BaseTools/Scripts/GccBase.lds">
https://github.com/tianocore/edk2/blob/master/BaseTools/Scripts/GccBase.lds</a> to do the linking. We use position independent code with hidden visibility to inform the compiler that symbol references are never resolved at runtime. My problem is I found after
 the lld linking with –pie enabled, the symbol values in .data section, which have the R_X86_64_64 relocation entries, are all 0. In other word, I found the S in below R_X86_64_64  calculation is 0.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
<tbody>
<tr>
<td width="188" style="width:141.0pt;border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"NimbusRomNo9L-Regu",serif;color:black">Name:</span>
<span style="font-size:9.0pt;font-family:"NimbusMonL-Regu",serif;color:black">R_X86_64_64
</span><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><o:p></o:p></span></p>
</td>
<td width="42" style="width:31.5pt;border:solid windowtext 1.0pt;border-left:none;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"NimbusRomNo9L-Regu",serif;color:black">1
</span><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><o:p></o:p></span></p>
</td>
<td width="67" style="width:50.25pt;border:solid windowtext 1.0pt;border-left:none;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><i><span style="font-size:9.0pt;font-family:"NimbusRomNo9L-ReguItal",serif;color:black">word64
</span></i><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><o:p></o:p></span></p>
</td>
<td width="150" style="width:112.5pt;border:solid windowtext 1.0pt;border-left:none;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"NimbusMonL-Regu",serif;color:black">S + A</span><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Below is an example to compare the lld and ld, sorry about the verbose.<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Firstly, I use lld to link a HelloWorld module with -pie enabled:<o:p></o:p></p>
<p class="MsoNormal">"/home/jshi19/llvm/releaseinstall/bin/<span style="color:red">ld.lld</span>"
<span style="color:red">-pie </span>-z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/DEBUG/HelloWorld.dll
 -u _ModuleEntryPoint -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0 -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../.. -L/home/jshi19/llvm/releaseinstall/bin/../lib
 -L/lib -L/usr/lib -q --gc-sections -z max-page-size=0x40 --entry _ModuleEntryPoint -Map /home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/DEBUG/HelloWorld.map --whole-archive -O0 -melf_x86_64 --oformat
 elf64-x86-64 --start-group @/home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/OUTPUT/static_library_files.lst --end-group --defsym=PECOFF_HEADER_SIZE=0x228 --script=/home/jshi19/wksp_efi/lgao4/edk2/BaseTools/Scripts/GccBase.lds<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Then, I check the R_X86_64_64 relocation entries in .rela.data section, and find their target offsets<o:p></o:p></p>
<p class="MsoNormal">$ readelf -r /home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/DEBUG/HelloWorld.dll<o:p></o:p></p>
<p class="MsoNormal">Relocation section '.rela.data' at offset 0x5b7e8 contains 41 entries:<o:p></o:p></p>
<p class="MsoNormal">  Offset          Info           Type           Sym. Value    Sym. Name + Addend<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">000000005040  </span>00d600000001 R_X86_64_64       0000000000003130 TestFunction1 + 0<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">000000005048  </span>00d700000001 R_X86_64_64       0000000000003150 TestFunction2 + 0<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">3.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Next, I check the symbol values in .data section which are targeted by above R_X86_64_64 relocatons<o:p></o:p></p>
<p class="MsoNormal">$ readelf -x2 HelloWorld.dll                                                     <o:p></o:p></p>
<p class="MsoNormal">Hex dump of section '.data':<o:p></o:p></p>
<p class="MsoNormal">NOTE: This section has relocations against it, but these have NOT been applied to this dump.<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal">  0x00005030 00000000 00000000 00000000 00000000 ................<o:p></o:p></p>
<p class="MsoNormal">  <span style="color:red">0x00005040 00000000 00000000 00000000 00000000
</span>................<o:p></o:p></p>
<p class="MsoNormal">  0x00005050 4ebe7903 06d77d43 b037edb8 2fb772a4 N.y...}C.7../.r.<o:p></o:p></p>
<p class="MsoNormal">  0x00005060 00000000 00000000 00000000 00000000 ................<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal">You can see the offset 0x5040 and 0x5048 symbol value are all 0, which is not correct.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But if I remove the -pie option in the above step 1 lld link command, the 0x5040 and 0x5048 symbol values are correct.<o:p></o:p></p>
<p class="MsoNormal">$ readelf -x2 HelloWorld.dll                                                     <o:p></o:p></p>
<p class="MsoNormal">Hex dump of section '.data':<o:p></o:p></p>
<p class="MsoNormal">NOTE: This section has relocations against it, but these have NOT been applied to this dump.<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal">  0x00005030 04420000 00000000 00000000 00000000 .B..............<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">  0x00005040 30310000 00000000 50310000 00000000 01......P1......<o:p></o:p></span></p>
<p class="MsoNormal">  0x00005050 4ebe7903 06d77d43 b037edb8 2fb772a4 N.y...}C.7../.r.<o:p></o:p></p>
<p class="MsoNormal">  0x00005060 00000000 00000000 00000000 00000000 ................<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">And if I replace lld with ld but still use exact same link options with –pie enabled, the R_X86_64_64 symbol values are correct.<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Link again with ld and same link options:<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">ld -pie </span>-z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/DEBUG/HelloWorld.dll
 -u _ModuleEntryPoint -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0 -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../.. -L/home/jshi19/llvm/releaseinstall/bin/../lib
 -L/lib -L/usr/lib -q --gc-sections -z max-page-size=0x40 --entry _ModuleEntryPoint -Map /home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/DEBUG/HelloWorld.map --whole-archive -O0 -melf_x86_64 --oformat
 elf64-x86-64 --start-group @/home/jshi19/wksp_efi/lgao4/edk2/Build/OvmfX64/NOOPT_CLANG38/X64/MdeModulePkg/Application/HelloWorld/HelloWorld/OUTPUT/static_library_files.lst --end-group --defsym=PECOFF_HEADER_SIZE=0x228 --script=/home/jshi19/wksp_efi/lgao4/edk2/BaseTools/Scripts/GccBase.lds<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Then, check the .rela.data section R_X86_64_64 relocation entries:<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">000000004f40  </span>00a400000001 R_X86_64_64       0000000000003130 TestFunction1 + 0<o:p></o:p></p>
<p class="MsoNormal"><span style="color:red">000000004f48  </span>009a00000001 R_X86_64_64       0000000000003150 TestFunction2 + 0<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l1 level1 lfo2"><![if !supportLists]><span style="mso-list:Ignore">3.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Check the R_X86_64_64 targeting symbol values in .data section<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal">  0x00004f30 f3410000 00000000 00000000 00000000 .A..............<o:p></o:p></p>
<p class="MsoNormal">  <span style="color:red">0x00004f40 30310000 00000000 50310000 00000000 01......P1......</span><o:p></o:p></p>
<p class="MsoNormal">  0x00004f50 00000000 00000000 00000000 00000000 ................<o:p></o:p></p>
<p class="MsoNormal">… …<o:p></o:p></p>
<p class="MsoNormal">You can see the offset 0x4f40 and 0x4f48 symbol value are not 0, which is correct.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Appreciate if you could give me some advices on how to let lld output correct symbol values when enable pie.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks<o:p></o:p></p>
<p class="MsoNormal">Steven<b><span style="font-size:12.0pt;font-family:"Arial",sans-serif"><o:p></o:p></span></b></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>