LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 25528 - lld -Bstatic fails to link because _end is undefined
Summary: lld -Bstatic fails to link because _end is undefined
Status: RESOLVED FIXED
Alias: None
Product: lld
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: PC Windows NT
: P normal
Assignee: Igor Kudrin
URL:
Keywords:
Depends on:
Blocks: 23214
  Show dependency tree
 
Reported: 2015-11-13 17:20 PST by Davide Italiano
Modified: 2015-11-19 20:39 PST (History)
6 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Davide Italiano 2015-11-13 17:20:56 PST
The first bug found while building world on FreeBSD =)

sbrk() access _end (directly!) to understand where .bss ends.
The linker should probably insert that in the generated executable (at least when -Bstatic is used). It seems gold doesn't strip the symbol even if not referenced.

 % ./clang sbrk.c -fuse-ld=lld2 -o sbrs -static
undefined symbol: _end in sbrk.o
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

Linker invocation:

 "/exps/llvm-lld/build/bin/./ld.lld2" -Bstatic -o sbrs /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbeginT.o -L/usr/lib /tmp/sbrk-63b6db.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/crtend.o /usr/lib/crtn.o


% cat sbrk.c

#include <sys/types.h>
#include <unistd.h>

int
main(void)
{
  void *me = sbrk(42);
  return (0);
}
Comment 1 Davide Italiano 2015-11-13 17:31:37 PST
Repro:

% objdump -t ./end | grep _end
[...]
0000000000665df0 g       *ABS*  0000000000000000 _end

% ./clang end.c -fuse-ld=lld2 -o blah -static
undefined symbol: _end in /tmp/blah-289342.o

% cat end.c
extern int _end;

int main(void)
{
  return (_end + 5);
}
Comment 2 Rui Ueyama 2015-11-18 16:50:59 PST
These linker-created symbols are similar to __init_array_end and the like but are different in such way that they are relative to segments. __init_array_end and the like are defined for sections.

We cannot calculate the address of _end symbol until we bin all sections into segments. But before doing that, all symbols need to be defined and available since we need to create GOT/PLT segments contents.

So I think this needs to be done in two-pass. In the first pass, we add _end symbol with a dummy address 0, and backfill that address once we create segments.
Comment 3 Igor Kudrin 2015-11-19 13:22:21 PST
http://reviews.llvm.org/D14833
Comment 4 Igor Kudrin 2015-11-19 20:39:25 PST
Should be fixed in r253637.