Skip to content

Commit 00ea0b1

Browse files
aykevldeadprogram
authored andcommitted
build: list libraries at the end of the linker command
Static libraries should be added at the end of the linker command, after all object files. If that isn't done, that's _usually_ not a problem, unless there are duplicate symbols. In that case, weird dependency issues can arise. To solve that, object files (that may include symbols to override symbols in the library) should be listed first on the command line and then the static libraries should be listed. This fixes an issue with overriding some symbols in wasi-libc.
1 parent efa0410 commit 00ea0b1

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

builder/build.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -470,33 +470,10 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
470470
linkerDependencies = append(linkerDependencies, job)
471471
}
472472

473-
// Add libc dependency if needed.
474-
root := goenv.Get("TINYGOROOT")
475-
switch config.Target.Libc {
476-
case "picolibc":
477-
job, err := Picolibc.load(config.Triple(), config.CPU(), dir)
478-
if err != nil {
479-
return err
480-
}
481-
// The library needs to be compiled (cache miss).
482-
jobs = append(jobs, job.dependencies...)
483-
jobs = append(jobs, job)
484-
linkerDependencies = append(linkerDependencies, job)
485-
case "wasi-libc":
486-
path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a")
487-
if _, err := os.Stat(path); os.IsNotExist(err) {
488-
return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?")
489-
}
490-
ldflags = append(ldflags, path)
491-
case "":
492-
// no library specified, so nothing to do
493-
default:
494-
return fmt.Errorf("unknown libc: %s", config.Target.Libc)
495-
}
496-
497473
// Add jobs to compile extra files. These files are in C or assembly and
498474
// contain things like the interrupt vector table and low level operations
499475
// such as stack switching.
476+
root := goenv.Get("TINYGOROOT")
500477
for _, path := range config.ExtraFiles() {
501478
abspath := filepath.Join(root, path)
502479
job := &compileJob{
@@ -537,6 +514,31 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
537514
ldflags = append(ldflags, lprogram.LDFlags...)
538515
}
539516

517+
// Add libc dependency if needed.
518+
switch config.Target.Libc {
519+
case "picolibc":
520+
job, err := Picolibc.load(config.Triple(), config.CPU(), dir)
521+
if err != nil {
522+
return err
523+
}
524+
// The library needs to be compiled (cache miss).
525+
jobs = append(jobs, job.dependencies...)
526+
jobs = append(jobs, job)
527+
linkerDependencies = append(linkerDependencies, job)
528+
case "wasi-libc":
529+
path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a")
530+
if _, err := os.Stat(path); os.IsNotExist(err) {
531+
return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?")
532+
}
533+
job := dummyCompileJob(path)
534+
jobs = append(jobs, job)
535+
linkerDependencies = append(linkerDependencies, job)
536+
case "":
537+
// no library specified, so nothing to do
538+
default:
539+
return fmt.Errorf("unknown libc: %s", config.Target.Libc)
540+
}
541+
540542
// Create a linker job, which links all object files together and does some
541543
// extra stuff that can only be done after linking.
542544
jobs = append(jobs, &compileJob{

0 commit comments

Comments
 (0)