Skip to content
This repository has been archived by the owner on Sep 2, 2018. It is now read-only.

Weakly undefined symbols #9

Open
dylanmckay opened this issue Sep 24, 2015 · 7 comments
Open

Weakly undefined symbols #9

dylanmckay opened this issue Sep 24, 2015 · 7 comments

Comments

@dylanmckay
Copy link
Member

Note: Migrated from here.

Weakly defined symbols like so (taken from Arduino):

// this next line disables the entire HardwareSerial.cpp, 
// this is so I can support Attiny series and any other chip without a uart
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)

// SerialEvent functions are weak, so when the user doesn't define them,
// the linker just sets their address to 0 (which is checked below).
// The Serialx_available is just a wrapper around Serialx.available(),
// but we can refer to it weakly so we don't pull in the entire
// HardwareSerial instance if the user doesn't also refer to it.
#if defined(HAVE_HWSERIAL0)
  void serialEvent() __attribute__((weak));
  bool Serial0_available() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL1)
  void serialEvent1() __attribute__((weak));
  bool Serial1_available() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL2)
  void serialEvent2() __attribute__((weak));
  bool Serial2_available() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL3)
  void serialEvent3() __attribute__((weak));
  bool Serial3_available() __attribute__((weak));
#endif

Are failing during the link stage due to undefined references to the weak symbols (by GNU's avr-ld). Weak symbols have the property that if they are not defined, they are set to null instead. It makes no sense for a weak undefined symbol, thus this error must lay with clang or llvm not placing the weak attribute in the ELF object file.

@4ntoine
Copy link

4ntoine commented Sep 25, 2015

MBA-Anton:bin asmirnov$ ./clang -v
clang version 3.8.0 (https:/avr-llvm/clang.git 3b2c949e21022c841c80fca5e79d1f1387cd012c) (llvm/llvm.git 47a798c880f7baaf6b80b99e8653a22a28efc025)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

@4ntoine
Copy link

4ntoine commented Sep 25, 2015

Here is HardwareSerial.cpp and HardwareSerial.ll (just to check if weak is absent in ll):
https://gist.github.com/4ntoine/f00d0651bdda6744e41e.

I've used

MBA-Anton:bin asmirnov$ ./clang -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include --target=avr /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp -o /tmp/arduino_test1/HardwareSerial.cpp.o

to get .ll from .cpp (just like original Arduino IDE does, except i've replaced -o /tmp/arduino_test1/HardwareSerial.cpp.o with -S -emit-llvm -o /tmp/arduino_test1/HardwareSerial.ll).

I'm able to find weak in .ll:
define weak void @_Z14serialEventRunv() #0 {

@4ntoine
Copy link

4ntoine commented Sep 25, 2015

also i can see some 'extern_weak' attributes:

; Function Attrs: optsize
declare extern_weak void @_Z11serialEventv() #0

@dylanmckay
Copy link
Member Author

This means that this is not a clang problem, but an LLVM problem.

@4ntoine
Copy link

4ntoine commented Oct 1, 2015

@dylanmckay
Copy link
Member Author

LLVM does support weak linkage here. Function signatures can also be annotated with weak and so LLVM does support it.

The problem must be deeper.

@4ntoine
Copy link

4ntoine commented Oct 1, 2015

as you can see in SO question, i've tried Apple's LLVM/Clang and non-avr sources and it can't link. I believe it's a bug, but i've asked the question to both cfe-users@ and cfe-dev@ maillists.

teto added a commit to teto/ns-3-dce that referenced this issue Aug 10, 2016
... manually with teh zsh script at the beginning. Ideally, it should be
run by waf.

Aliases are disabled for now while I confirm clang support for weak
linking.
avr-llvm/clang#9
teto added a commit to teto/ns-3-dce that referenced this issue Nov 11, 2016
... manually with teh zsh script at the beginning. Ideally, it should be
run by waf.

Aliases are disabled for now while I confirm clang support for weak
linking.
avr-llvm/clang#9
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants