amba start.S 分析 .weak作用

來源:互聯網
上載者:User

我們用nm看動態庫時,會發現有些符號類型是"V",手冊裡解釋如下:

"V" The symbol is a weak
object.  When a weak defined symbol is linked with a normal  defined 
symbol, the normal defined symbol is used with no error. When a weak
undefined symbol is linked and the symbol is not defined, the value of
the weak symbol becomes zero with no error.
說的是動態庫中的weak symbol,預設會被normal symbol替代,如果沒有定義,則該symbol的值為0。
很抽象,是不是,我一直想找一個簡單的例子。
             
最近看過一篇文章:
http://www.cs.virginia.edu/~wh5a/blog/2006/07/20/the-weak-attribute-of-gcc/


終於對所謂的weak symbol有了一點瞭解。
http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Function-Attributes.html


講了__attribute__的文法。 【weak.c】
extern void foo() __attribute__((weak));int main() {
  if (foo) foo();
}
程式居然能夠編譯通過,甚至成功執行!讓我們來看看是為什嗎?
首先聲明了一個符號foo(),屬性為weak,但並不定義它,這樣,連結器會將此未定義的weak symbol賦值為0,也就是說foo()並沒有真正被調用,試試看,去掉if條件,肯定core dump! 【strong.c】
extern void foo() ;int main() {
  if (foo) foo();
}
這個是一般程式,編譯過不了:
strong.c: undefined reference to `foo'再添上一個定義檔案:【foo.c】
void foo() {
  printf("in foo./n");
}
OK! 用nm檢查一下:
linux:~/test/weak # nm weak.o
         w foo
00000000 T main
linux:~/test/weak # nm foo.o
00000000 T foo
         U printf
連結時,前面那個weak symbol會被後面這個代替,如果沒有連結foo.o,也沒問題,對應符號為0。 這就是weak symbol的意義。-------------------------------------------------------------------------------------------------------------------------------------------

`V' The symbol is a weak object.  When a weak defined symbol is
linked with a normal defined  symbol, the  normal defined symbol is
used with no error.  When a weak undefined symbol is linked and the
symbol is not defined, the value of the weak symbol becomes zero with
no error.
`W' The symbol is a weak symbol that has not been
specifically tagged as a weak object symbol.   When a  weak  defined
symbol is linked with a normal defined symbol, the normal defined
symbol is used with no error.  When a weak undefined symbol is linked
and the symbol is not defined,  the  value of the weak symbol becomes
zero with no error.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.