Skip to content

[XCOFF] make related SD symbols as isFunction #69553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/Object/XCOFFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,9 @@ class xcoff_symbol_iterator : public symbol_iterator {
xcoff_symbol_iterator(const basic_symbol_iterator &B)
: symbol_iterator(B) {}

xcoff_symbol_iterator(const XCOFFSymbolRef *Symbol)
: symbol_iterator(*Symbol) {}

const XCOFFSymbolRef *operator->() const {
return static_cast<const XCOFFSymbolRef *>(symbol_iterator::operator->());
}
Expand Down
56 changes: 46 additions & 10 deletions llvm/lib/Object/XCOFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,21 +1242,57 @@ Expected<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 an external
// symbol.
if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM ||
CsectAuxRef.getSymbolType() == XCOFF::XTY_ER)
return false;

const int16_t SectNum = getSectionNumber();
Expected<DataRefImpl> SI = getObject()->getSectionByNum(SectNum);
if (!SI)
return SI.takeError();
// If the next symbol is an XTY_LD type symbol with the 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 work around the fact that LLVM always generates below
// symbol for -ffunction-sections:
// m 0x00000000 .text 1 unamex **No Symbol**
// a4 0x00000000 0 0 SD PR 0 0
// FIXME: remove or replace this meaningless symbol.
if (getSize() == 0)
return false;

xcoff_symbol_iterator NextIt(this);
// If this is the last main symbol table entry, there won't be an XTY_LD
// type symbol below.
if (++NextIt == getObject()->symbol_end())
return true;

if (cantFail(getAddress()) != cantFail(NextIt->getAddress()))
return true;

// Check next symbol is XTY_LD. If so, this symbol is not a function.
Expected<XCOFFCsectAuxRef> NextCsectAuxEnt = NextIt->getXCOFFCsectAuxRef();
if (!NextCsectAuxEnt)
return NextCsectAuxEnt.takeError();

if (NextCsectAuxEnt.get().getSymbolType() == XCOFF::XTY_LD)
return false;

return (getObject()->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
return true;
}

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

Copy link
Contributor

@diggerlin diggerlin Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current implement, I do not think the following code(which check for section) 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;

Copy link
Collaborator Author

@chenzheng1030 chenzheng1030 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think below codes seem not be needed even before this change. I don't think a XCOFF::XTY_LD symbol with mapping class XMC_PR does not belong to .text section is a valid input? At least clang XCOFFObjectWriter or commercial XLC will not generate such symbol?

If so, maybe we can add an NFC patch to remove this?

Copy link
Contributor

@diggerlin diggerlin Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think below codes seem not be needed even before this change. I don't think a XCOFF::XTY_LD symbol with mapping class XMC_PR does not belong to .text section is a valid input? At least clang XCOFFObjectWriter or commercial XLC will not generate such symbol?

If so, maybe we can add an NFC patch to remove this?

since you almost rewrite the isFunction(), I think we can delete the code in the PR, what is your option @jh7370 , @stephenpeckham

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got no particularly strong preference either way, but if there's a clear improvement that could be made without this patch, I don't think it hurts spinning it off into its own PR.

return createError(
"symbol csect aux entry with index " +
Twine(getObject()->getSymbolIndex(CsectAuxRef.getEntryAddress())) +
" has invalid symbol type " +
Twine::utohexstr(CsectAuxRef.getSymbolType()));
}

bool XCOFFSymbolRef::isCsectSymbol() const {
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/CodeGen/PowerPC/aix-text.ll
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ entry:
ret i32 2
}

; CHECKFS32: 00000000 l .text 00000000 (idx: {{[[:digit:]]*}}) [PR]
; CHECKFS32-NEXT: 00000000 g .text {{([[:xdigit:]]{8})}} (idx: {{[[:digit:]]*}}) .text[PR]
; CHECKFS32-NEXT: {{([[:xdigit:]]{8})}} g .text {{([[:xdigit:]]{8})}} (idx: {{[[:digit:]]*}}) .text2[PR]
; CHECKFS32: 00000000 l .text 00000000 (idx: {{[[:digit:]]*}}) [PR]
; CHECKFS32-NEXT: 00000000 g F .text {{([[:xdigit:]]{8})}} (idx: {{[[:digit:]]*}}) .text[PR]
; CHECKFS32-NEXT: {{([[:xdigit:]]{8})}} g F .text {{([[:xdigit:]]{8})}} (idx: {{[[:digit:]]*}}) .text2[PR]

; CHECKFS64: 0000000000000000 l .text 0000000000000000
; CHECKFS64-NEXT: 0000000000000000 g .text {{([[:xdigit:]]{16})}} (idx: {{[[:digit:]]*}}) .text[PR]
; CHECKFS64-NEXT: {{([[:xdigit:]]{16})}} g .text {{([[:xdigit:]]{16})}} (idx: {{[[:digit:]]*}}) .text2[PR]
; CHECKFS64: 0000000000000000 l .text 0000000000000000
; CHECKFS64-NEXT: 0000000000000000 g F .text {{([[:xdigit:]]{16})}} (idx: {{[[:digit:]]*}}) .text[PR]
; CHECKFS64-NEXT: {{([[:xdigit:]]{16})}} g F .text {{([[:xdigit:]]{16})}} (idx: {{[[:digit:]]*}}) .text2[PR]

; CHECK32: 00000000 l .text {{([[:xdigit:]]{8})}} (idx: {{[[:digit:]]*}}) [PR]
; CHECK32-NEXT: {{([[:xdigit:]]{8})}} g F .text (csect: (idx: {{[[:digit:]]*}}) [PR]) 00000000 (idx: {{[[:digit:]]*}}) .text
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/CodeGen/PowerPC/aix-xcoff-funcsect.ll
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ entry:
; XCOFF32-NEXT: 00000000 l .text 00000000 (idx: 5) [PR]
; XCOFF32-NEXT: 00000000 g .text 00000019 (idx: 7) .foo[PR]
; XCOFF32-NEXT: 00000000 g F .text (csect: (idx: 7) .foo[PR]) 00000000 (idx: 9) .alias_foo
; XCOFF32-NEXT: 00000020 g .text 00000020 .hidden (idx: 11) .hidden_foo[PR]
; XCOFF32-NEXT: 00000040 g .text 00000059 (idx: 13) .bar[PR]
; XCOFF32-NEXT: 000000c0 l .text 0000002a (idx: 15) .static_overalign_foo[PR]
; XCOFF32-NEXT: 00000020 g F .text 00000020 .hidden (idx: 11) .hidden_foo[PR]
; XCOFF32-NEXT: 00000040 g F .text 00000059 (idx: 13) .bar[PR]
; XCOFF32-NEXT: 000000c0 l F .text 0000002a (idx: 15) .static_overalign_foo[PR]
; XCOFF32-NEXT: 000000ec g O .data 0000000c (idx: 17) foo[DS]
; XCOFF32-NEXT: 000000ec g O .data (csect: (idx: 17) foo[DS]) 00000000 (idx: 19) alias_foo
; XCOFF32-NEXT: 000000f8 g O .data 0000000c .hidden (idx: 21) hidden_foo[DS]
Expand Down Expand Up @@ -152,9 +152,9 @@ entry:
; XCOFF64-NEXT: 0000000000000000 l .text 0000000000000000 (idx: 5) [PR]
; XCOFF64-NEXT: 0000000000000000 g .text 0000000000000019 (idx: 7) .foo[PR]
; XCOFF64-NEXT: 0000000000000000 g F .text (csect: (idx: 7) .foo[PR]) 0000000000000000 (idx: 9) .alias_foo
; XCOFF64-NEXT: 0000000000000020 g .text 0000000000000020 .hidden (idx: 11) .hidden_foo[PR]
; XCOFF64-NEXT: 0000000000000040 g .text 0000000000000059 (idx: 13) .bar[PR]
; XCOFF64-NEXT: 00000000000000c0 l .text 000000000000002a (idx: 15) .static_overalign_foo[PR]
; XCOFF64-NEXT: 0000000000000020 g F .text 0000000000000020 .hidden (idx: 11) .hidden_foo[PR]
; XCOFF64-NEXT: 0000000000000040 g F .text 0000000000000059 (idx: 13) .bar[PR]
; XCOFF64-NEXT: 00000000000000c0 l F .text 000000000000002a (idx: 15) .static_overalign_foo[PR]
; XCOFF64-NEXT: 00000000000000f0 g O .data 0000000000000018 (idx: 17) foo[DS]
; XCOFF64-NEXT: 00000000000000f0 g O .data (csect: (idx: 17) foo[DS]) 0000000000000000 (idx: 19) alias_foo
; XCOFF64-NEXT: 0000000000000108 g O .data 0000000000000018 .hidden (idx: 21) hidden_foo[DS]
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/tools/llvm-objdump/XCOFF/aux-entry-invalid-type.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Check that llvm-objdump --syms reports an error when
## the symbol type in the csect aux entry of a symbol is not valid.

## Check XCOFF32
# RUN: yaml2obj -DMAGICNUMBER=0x1DF %s -o %t1
# RUN: not llvm-objdump --syms %t1 2>&1 | FileCheck %s -DOBJ=%t1

## Check XCOFF64
# RUN: yaml2obj -DMAGICNUMBER=0x1F7 %s -o %t2
# RUN: not llvm-objdump --syms %t2 2>&1 | FileCheck %s -DOBJ=%t2

# CHECK: error: '[[OBJ]]': symbol csect aux entry with index 2 has invalid symbol type 5

--- !XCOFF
FileHeader:
MagicNumber: [[MAGICNUMBER]]
Sections:
- Name: .text
Flags: [ STYP_TEXT ]
Symbols:
- Name: .file
Section: N_DEBUG
NumberOfAuxEntries: 0
Type: 0x0
StorageClass: C_FILE
- Name: test
Section: .text
NumberOfAuxEntries: 1
StorageClass: C_EXT
AuxEntries:
- Type: AUX_CSECT
SymbolAlignmentAndType: 5
StorageMappingClass: XMC_PR
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-symbolizer/xcoff-sd-symbol.ll
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ entry:
ret void
}

; CHECK: ??
; CHECK: .foo
; CHECK: ??:0:0
; CHECK-EMPTY:

; CHECK: ??
; CHECK: .foo1
; CHECK: ??:0:0
; CHECK-EMPTY: