Skip to content
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

[lld] Why use PROVIDE_HIDDEN defined symbol in linker script is GLOBAL when partial linking? #92116

Closed
hstk30-hw opened this issue May 14, 2024 · 4 comments
Labels
lld question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@hstk30-hw
Copy link
Contributor

lds:

SECTIONS
{
  . mySec:
  {
    PROVIDE_HIDDEN(__mysec_start = .);
    KEEP (*(SORT(.probs*)))
    PROVIDE_HIDDEN(__mysec_end = .);
  }
};

main.c

extern void *__mysec_start;
extern void *__mysec_end;

int getNum(void) {
    return &__mysec_end - &__mysec_start;
}

int main() {
  return getNum();
}

Command like:

clang -c main.c -o main.o
ld.lld main.o -o main -T lds -r

And the symbol info like:

 8: 0000000000000000     0 NOTYPE  GLOBAL HIDDEN     1 __mysec_end
 9: 0000000000000000     0 NOTYPE  GLOBAL HIDDEN     1 __mysec_start

without -r the output like:

 8: 0000000000000000     0 NOTYPE  LOCAL HIDDEN     1 __mysec_end
 9: 0000000000000000     0 NOTYPE  LOCAL HIDDEN     1 __mysec_start

And ld always LOCAL w/wo -r

gold is GLOBAL with -r.

I'm not found the standard about this (about the bindind of symbol defined in linker script).

@github-actions github-actions bot added the lld label May 14, 2024
@hstk30-hw
Copy link
Contributor Author

CC @rui314 @MaskRay

@MaskRay
Copy link
Member

MaskRay commented May 15, 2024

The generic ABI says:

A hidden symbol contained in a relocatable object must be either removed or converted to STB_LOCAL binding by the link-editor when the relocatable object is included in an executable file or shared object.

lld converts hidden symbols to STB_LOCAL when producing an EXE/DSO. GNU ld and gold don't do this.
(gold seems inconsistent. It creates STB_LOCAL STV_HIDDEN _GLOBAL_OFFSET_TABLE_)

@MaskRay MaskRay added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label May 15, 2024
@hstk30-hw
Copy link
Contributor Author

In my example, the symbol is defined in linker script, not in a relocatable object. It's a trick :)

So, it seems lld not follow the rule. lld should output LOCAL HIDDEN symbol like ld?

@MaskRay
Copy link
Member

MaskRay commented May 15, 2024

lld actually follows the rule but GNU ld/gold don't.

Keeping a global symbol in relocatable output allows ld -r a.o b.o -o ab.ro; ld ab.o c.o to have a similar behavior to ld a.o b.o c.o.

@MaskRay MaskRay closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lld question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

2 participants