[llvm] [XCOFF] make related SD symbols as isFunction (PR #69553)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 31 07:13:33 PDT 2023


================
@@ -1242,15 +1242,72 @@ bool XCOFFSymbolRef::isFunction() const {
 
   const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get();
 
-  // A function definition should be a label definition.
-  // FIXME: This is not necessarily the case when -ffunction-sections is
-  // enabled.
-  if (!CsectAuxRef.isLabel())
+  if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR &&
+      CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_GL)
     return false;
 
-  if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR)
+  // A function definition should not be a common type symbol or a external
+  // symbol.
+  if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM ||
+      CsectAuxRef.getSymbolType() == XCOFF::XTY_ER)
     return false;
 
+  // If the next symbol is a XTY_LD type symbol with same address, this XTY_SD
+  // symbol is not a function. Otherwise this is a function symbol for
+  // -ffunction-sections.
+  if (CsectAuxRef.getSymbolType() == XCOFF::XTY_SD) {
+    // If this is a csect with size 0, it won't be a function definition.
+    // This is used to hack situation that llvm always generates below symbol
+    // for -ffunction-sections:
+    // m   0x00000000     .text     1  unamex                    **No Symbol**
+    // a4  0x00000000       0    0     SD       PR    0    0
+    if (getSize() == 0)
+      return false;
+
+    uint8_t NumberOfAuxEntries = getNumberOfAuxEntries();
+
+    // If this is the last main symbol table entry, there won't be XTY_LD type
+    // symbol below.
+    if (getEntryAddress() == getObject()->getSymbolEntryAddressByIndex(
+                                 getObject()->getNumberOfSymbolTableEntries() -
+                                 NumberOfAuxEntries - 1))
+      return true;
+
+    DataRefImpl Ref;
+    Ref.p = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+        getEntryAddress(), NumberOfAuxEntries + 1);
+    XCOFFSymbolRef NextSym = getObject()->toSymbolRef(Ref);
+    if (!NextSym.isCsectSymbol())
+      return true;
+
+    Expected<uint64_t> SymbolAddressOrErr = getAddress();
+    if (!SymbolAddressOrErr)
+      return false;
+
+    Expected<uint64_t> NextSymbolAddressOrErr = NextSym.getAddress();
+    if (!NextSymbolAddressOrErr)
+      return true;
+
+    if (SymbolAddressOrErr.get() != NextSymbolAddressOrErr.get())
+      return true;
+
+    // Check next symbol is XTY_LD. If so, this symbol is not a function.
+    Expected<XCOFFCsectAuxRef> NextCsectAuxEnt = NextSym.getXCOFFCsectAuxRef();
+    if (!NextCsectAuxEnt) {
+      // If the next symbol has no aux entries, won't be a XTY_LD symbol.
+      consumeError(NextCsectAuxEnt.takeError());
+      return true;
+    }
+
+    if (NextCsectAuxEnt.get().getSymbolType() == XCOFF::XTY_LD)
+      return false;
+
+    return true;
+  }
+
+  if (CsectAuxRef.getSymbolType() == XCOFF::XTY_LD)
+    return true;
+
----------------
diggerlin wrote:

In current code, I do not think the following code will be running.
The reason as :
```

if (!isCsectSymbol())
    return false; 

 //the following deals with has AuxiCSect   ( there are 4 symbol type in csect Auxiliary Entry,  which all has return in the following code )
 
   if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM ||
      CsectAuxRef.getSymbolType() == XCOFF::XTY_ER)
    return false;


  if (CsectAuxRef.getSymbolType() == XCOFF::XTY_SD) {
    ...
    return true;
} 

  if (CsectAuxRef.getSymbolType() == XCOFF::XTY_LD)
    return true;
```
     

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


More information about the llvm-commits mailing list