This is an archive of the discontinued LLVM Phabricator instance.

[XCOFF] handle the toc-data for object file generation.
ClosedPublic

Authored by Esme on Dec 7 2022, 1:19 AM.

Details

Summary

The toc-data feature has been supported for assembly file generation. This patch handles the toc-data for object file generation.

Diff Detail

Event Timeline

Esme created this revision.Dec 7 2022, 1:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2022, 1:19 AM
Esme requested review of this revision.Dec 7 2022, 1:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2022, 1:19 AM
shchenz added inline comments.Dec 23 2022, 1:29 AM
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
2334

Same as below, getExplicitSectionGlobal will not pass external globals.

2382

The caller of this function SectionForGlobal is not allowed to pass external (or available externally) globals., So I don't think we need to care about XTY_ER type symbol here. See:

/// This method computes the appropriate section to emit the specified global
/// variable or function definition. This should not be passed external (or
/// available externally) globals.
MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
                            const TargetMachine &TM) const;

We should handle the section for external TD symbols in getSectionForExternalReference() and adjust the order when we create the symbol TargetLoweringObjectFileXCOFF::getTargetSymbol(), i.e., handle external symbol first.

llvm/lib/MC/XCOFFObjectWriter.cpp
465–466

This can be put together with previous XMC_TC/XMC_TE case

633

This must also be wrong now as for TOC direct external symbols, there will be no TOC slot for them, so the FiexedValue must always be 0. For example, for below case:

int a = 20;
extern int c;
extern int b;
int d = 10;

int foo(void)
{
  return a + b + c + d;
}

I get:

00000000 <.foo>:
       0: 38 62 00 00  	addi 3, 2, 0
			00000002:  R_TOC	a
       4: 38 82 ff ac  	addi 4, 2, -84
			00000006:  R_TOC	b
       8: 38 a2 ff ac  	addi 5, 2, -84
			0000000a:  R_TOC	c
       c: 38 c2 00 04  	addi 6, 2, 4
			0000000e:  R_TOC	d

a and d are correct, but b/c are obvious wrong.

Esme updated this revision to Diff 486457.Jan 4 2023, 8:29 PM

Addressed comments except for the FixupValue for external TOC data.

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
2382

Thanks, it makes sense more.

llvm/lib/MC/XCOFFObjectWriter.cpp
633

It seems that the behavior of AIX's as is the same? So I'm a bit confused about this.

shchenz added inline comments.Jan 4 2023, 8:44 PM
llvm/lib/MC/XCOFFObjectWriter.cpp
633

Hmm, I made a test, with the assembly path, I get:

00000000 <.foo>:
       0: 38 62 00 00  	addi 3, 2, 0
			00000002:  R_TOC	a
       4: 80 63 00 00  	lwz 3, 0(3)
       8: 38 82 00 00  	addi 4, 2, 0 ====> The fixed value is 0 here
			0000000a:  R_TOC	b
       c: 80 84 00 00  	lwz 4, 0(4)
      10: 7c 63 22 14  	add 3, 3, 4
      14: 38 82 00 00  	addi 4, 2, 0 ====> The fixed value is 0 here
			00000016:  R_TOC	c
      18: 80 84 00 00  	lwz 4, 0(4)
      1c: 7c 63 22 14  	add 3, 3, 4
      20: 38 82 00 04  	addi 4, 2, 4
			00000022:  R_TOC	d
      24: 80 84 00 00  	lwz 4, 0(4)
      28: 7c 63 22 14  	add 3, 3, 4
      2c: 4e 80 00 20  	blr

This should be right.

Esme updated this revision to Diff 487275.Jan 8 2023, 9:06 PM

Addressed comments.
Changed the Fixup Value for TOC direct external symbols and added test lines for it in toc-data-const.ll.

shchenz accepted this revision as: shchenz.Jan 9 2023, 3:50 AM

Looks right now. Thanks for adding this support.

llvm/lib/MC/XCOFFObjectWriter.cpp
634

Can we add comments here like: "For non toc-data external symbols, R_TOC type relocation will relocate to data symbols that have XCOFF::XTY_SD type csect. For toc-data external symbols, R_TOC type relocation will relocate to data symbols that have XCOFF_ER type csect. For XCOFF_ER kind symbols, there will be no TOC entry for them, so the FixedValue should always be 0."?

This revision is now accepted and ready to land.Jan 9 2023, 3:50 AM
This revision was landed with ongoing or failed builds.Jan 11 2023, 8:28 PM
This revision was automatically updated to reflect the committed changes.