[llvm] [Mips] Fix clang integrated assembler generates incorrect relocations… (PR #83115)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 01:47:08 PDT 2024


yingopq wrote:

> > @wzssyqa @topperc @MaskRay Could you give me some advice? I did not konw if we should change asm scan beginning point or scan twice unconditionally. Thanks!
> 
> I think that you can try to figure out a patch first. So that we can run some test. Please go ahead.

@wzssyqa I tested with this patch about scaning twice, the result of ```readelf -r``` would occur double entries than normal, could you have some advice? 
```diff
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 76a3e501f459..36c3f550e320 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -997,6 +997,9 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
 
   getTargetParser().onBeginOfFile();
 
+  int cnt = 0;
+  SMLoc IDLoc_start = getTok().getLoc();
+A:
   // While we have input, parse each statement.
   while (Lexer.isNot(AsmToken::Eof)) {
     ParseStatementInfo Info(&AsmStrRewrites);
@@ -1017,6 +1020,13 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
       eatToEndOfStatement();
   }
 
+  cnt++;
+  if (cnt == 1) {
+    jumpToLoc(IDLoc_start);
+    Lex();
+    goto A;
+  }
+
   getTargetParser().onEndOfFile();
   printPendingErrors();
 
diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 176d55aa890b..e6071abc02ed 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -42,7 +42,7 @@
 #include <utility>
 
 using namespace llvm;
-
+#define DEBUG_TYPE "mips-mcstreamer"
 MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
   S.setTargetStreamer(this);
 }
@@ -424,9 +424,11 @@ void MCStreamer::assignFragment(MCSymbol *Symbol, MCFragment *Fragment) {
 void MCStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
   Symbol->redefineIfPossible();
 
-  if (!Symbol->isUndefined() || Symbol->isVariable())
-    return getContext().reportError(Loc, "symbol '" + Twine(Symbol->getName()) +
-                                             "' is already defined");
+  if (!Symbol->isUndefined() || Symbol->isVariable()) {
+    LLVM_DEBUG(dbgs() << DEBUG_TYPE << "symbol '" + Twine(Symbol->getName()) +
+                                             "' is already defined" << "\n");
+    return;
+  }
 
   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
   assert(getCurrentSectionOnly() && "Cannot emit before setting section!");

```

```
$ readelf -r 3.o

Relocation section '.rel.text' at offset 0x1b0 contains 11 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000000  00000405 R_MIPS_HI16       00000000   _gp_disp
00000004  00000406 R_MIPS_LO16       00000000   _gp_disp
0000001c  0000050b R_MIPS_CALL16     00000000   printf
00000020  00000525 R_MIPS_JALR       00000000   printf
0000003c  00000405 R_MIPS_HI16       00000000   _gp_disp
00000040  00000406 R_MIPS_LO16       00000000   _gp_disp
00000018  00000209 R_MIPS_GOT16      00000000   .rodata
00000054  00000209 R_MIPS_GOT16      00000000   .rodata
00000058  00000206 R_MIPS_LO16       00000000   .rodata
0000005c  0000050b R_MIPS_CALL16     00000000   printf
00000060  00000525 R_MIPS_JALR       00000000   printf

Relocation section '.rel.pdr' at offset 0x208 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000000  00000302 R_MIPS_32         0000003c   main
00000020  00000302 R_MIPS_32         0000003c   main

```

https://github.com/llvm/llvm-project/pull/83115


More information about the llvm-commits mailing list