GrammaTech / gtirb-rewriting

Python API for rewriting GTIRB files
GNU General Public License v3.0
16 stars 3 forks source link

Support for using TLS symbol from other module #3

Closed StarGazerM closed 2 years ago

StarGazerM commented 2 years ago

Hi! I meet a problem when I try to insert code contain TLS gobal variable in other module(for example during reassemble to binary phase I want to link and compile with another object file foo.o). For example:

movq %rax,  foo@gottroff(%rip)

seems current rewriter can't support tls attribute now? Is it possible to do this kind of rewriting, if I directly modify the GTIRB IR?

jranieri-grammatech commented 2 years ago

I assume you mean foo@gottpoff. gtirb-rewriting doesn't support it yet, but it should be really easy to add. Here's an untested diff that I can try to get in later this week.

diff --git a/gtirb_rewriting/assembler.py b/gtirb_rewriting/assembler.py
index 017797c..bd092e6 100644
--- a/gtirb_rewriting/assembler.py
+++ b/gtirb_rewriting/assembler.py
@@ -482,8 +482,29 @@ class Assembler:
         if "variantKind" in expr:
             if expr["variantKind"] == "PLT":
                 attributes.add(gtirb.SymbolicExpression.Attribute.PltRef)
+            elif expr["variantKind"] == "GOTNTPOFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.GotOff)
+                attributes.add(gtirb.SymbolicExpression.Attribute.NtpOff)
+            elif expr["variantKind"] == "GOT":
+                attributes.add(gtirb.SymbolicExpression.Attribute.GotOff)
+                attributes.add(gtirb.SymbolicExpression.Attribute.GotRef)
+            elif expr["variantKind"] == "GOTOFFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.GotOff)
+            elif expr["variantKind"] == "GOTTPOFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.GotRelPC)
+                attributes.add(gtirb.SymbolicExpression.Attribute.TpOff)
             elif expr["variantKind"] == "GOTPCREL":
                 attributes.add(gtirb.SymbolicExpression.Attribute.GotRelPC)
+            elif expr["variantKind"] == "TPOFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.TpOff)
+            elif expr["variantKind"] == "NTPOFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.NtpOff)
+            elif expr["variantKind"] == "DTPOFF":
+                attributes.add(gtirb.SymbolicExpression.Attribute.DtpOff)
+            elif expr["variantKind"] == "TLSGD":
+                attributes.add(gtirb.SymbolicExpression.Attribute.TlsGd)
+            elif expr["variantKind"] == "TLSLD":
+                attributes.add(gtirb.SymbolicExpression.Attribute.TlsLd)
             else:
                 assert False, f"Unsupported variantKind: {expr['variantKind']}"
         elif (
StarGazerM commented 2 years ago

okk, I had thought it is something like

gtirb.SymbolicExpression.Attribute.GOTTPOFF

(TvT)./

Thank you so much!

jranieri-grammatech commented 2 years ago

This has landed in gtirb-rewriting 0.1.0, which was just released.