<div dir="ltr">Hi Davide, thank you for the answer.<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">Therefore, if you have GVs with internal linkage when you run</span><br style="font-size:12.8px"><span style="font-size:12.8px">llvm-extract that information is lost.</span></blockquote><div><br></div><div>Which means that if I try to link another module I may have overlapping</div><div>variables?</div><div>My apologies but my knowledge of the linker is not as solid as I  wished.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">At least, you may want to fix this, the relevant code is around here</span><br style="font-size:12.8px"><span style="font-size:12.8px">(Transforms/IPO/ExtractGV.cpp)</span></blockquote><div><br></div><div>I will take a close look and do some experimentation. </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">I forgot, but apparently I had a bug open about this a while ago</span><br style="font-size:12.8px"><a href="https://bugs.llvm.org/show_bug.cgi?id=31674" rel="noreferrer" target="_blank" style="font-size:12.8px">https://bugs.llvm.org/show_<wbr>bug.cgi?id=31674</a><br></blockquote><div><br></div><div>I believe this falls under Gordon's comment right?</div><div>Do we really want to extract this variable with the function?</div><div><br></div><div>Quick question: What internalization means on this scope?</div><div><br></div><div>- nico</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-08-30 14:26 GMT-05:00 Davide Italiano <span dir="ltr"><<a href="mailto:davide@freebsd.org" target="_blank">davide@freebsd.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tue, Aug 29, 2017 at 3:10 PM, Nicolas Agostini via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
> Hi all,<br>
> First post to the list, I hope you can help or guide me on this task.<br>
><br>
> I am involved in a project that requires to re-link extracted and edited IR<br>
> code<br>
><br>
> Thus I want to know if these tools can be used in this way?<br>
><br>
> clang++-4.0 code03.cpp -emit-llvm -S -o code03.ll<br>
> llvm-extract-4.0 code03.ll -func main -S -o extracted_main.ll<br>
> llvm-link-4.0 code03.ll -only-needed -override extracted_main.ll -S -o<br>
> linked_main.ll<br>
> clang++-4.0 linked_main.ll -o main.out<br>
><br>
><br>
> where code03.cpp is:<br>
><br>
>> #include <iostream><br>
>> using namespace std;<br>
>> int main()<br>
>> {<br>
>>   cout << "First Message\n ";<br>
>>   cout << "Second Message\n ";<br>
>>   cout << "Third Message\n ";<br>
>>   return 0;<br>
>> }<br>
><br>
><br>
><br>
> I have been trying to extract a function's llvm IR, modify it preserving its<br>
> signature (or not), and re-insert this function back to the original IR<br>
> file, however I am getting an error during the compilation step (<br>
> clang++-4.0 linked_main.ll -o main.out ):<br>
><br>
>> main.ll:(.text+0x14): undefined reference to `.str'<br>
>> main.ll:(.text+0x34): undefined reference to `.str.1'<br>
>> main.ll:(.text+0x51): undefined reference to `.str.2'<br>
><br>
><br>
>  and linked_main.ll file has this section:<br>
><br>
>> @.str.4 = private unnamed_addr constant [16 x i8] c"First Message\0A \00",<br>
>> align 1<br>
>> @.str.1.6 = private unnamed_addr constant [17 x i8] c"Second Message\0A<br>
>> \00", align 1<br>
>> @.str.2.8 = private unnamed_addr constant [16 x i8] c"Third Message\0A<br>
>> \00", align 1<br>
>> @.str = external hidden unnamed_addr constant [16 x i8], align 1<br>
>> @.str.1 = external hidden unnamed_addr constant [17 x i8], align 1<br>
>> @.str.2 = external hidden unnamed_addr constant [16 x i8], align 1<br>
><br>
><br>
><br>
> But the function does not use the correct versions of the strings as the<br>
> linked "extracted_main" keeps making calls to .str, .str.1, .str.2? Am I not<br>
> supposed to do it this way?<br>
><br>
<br>
</div></div>llvm-extract changes the semantic as it gives every GlobalValue<br>
external linkage for simplicity.<br>
Therefore, if you have GVs with internal linkage when you run<br>
llvm-extract that information is lost.<br>
At least, you may want to fix this, the relevant code is around here<br>
(Transforms/IPO/ExtractGV.cpp)<br>
<br>
```<br>
      // For simplicity, just give all GlobalValues ExternalLinkage. A trickier<br>
      // implementation could figure out which GlobalValues are actually<br>
      // referenced by the Named set, and which GlobalValues in the rest of<br>
      // the module are referenced by the NamedSet, and get away with leaving<br>
      // more internal and private things internal and private. But for now,<br>
      // be conservative and simple.<br>
<br>
      // Visit the GlobalVariables.<br>
      for (Module::global_iterator I = M.global_begin(), E = M.global_end();<br>
           I != E; ++I) {<br>
        bool Delete =<br>
            deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();<br>
        if (!Delete) {<br>
          if (I-><wbr>hasAvailableExternallyLinkage(<wbr>))<br>
            continue;<br>
          if (I->getName() == "llvm.global_ctors")<br>
            continue;<br>
        }<br>
```<br>
<br>
Thanks,<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Davide<br>
<br>
"There are no solved problems; there are only problems that are more<br>
or less solved" -- Henri Poincare<br>
</font></span></blockquote></div><br></div>