{"id":899,"date":"2022-08-30T15:09:02","date_gmt":"2022-08-30T15:09:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-can-adding-a-function-call-cause-other-symbols-to-become-undefined-when-linking-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:02","modified_gmt":"2022-08-30T15:09:02","slug":"how-can-adding-a-function-call-cause-other-symbols-to-become-undefined-when-linking-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-can-adding-a-function-call-cause-other-symbols-to-become-undefined-when-linking-collection-of-common-programming-errors\/","title":{"rendered":"How can adding a function call cause other symbols to become undefined when linking?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m hoping someone will be able to help troubleshoot what I think is a linker script issue.<\/p>\n<p>I&#8217;m encountering a strange problem after adding a call to a new function. Without the function call, my object files link correctly, however, with the new function call added, I get an undefined reference to a symbol from another object file (I&#8217;ve verified it is actually present using objdump).<\/p>\n<p>Also strangely, with the function call present, if I link all object files first using ld -r (to give a relocatable output) and then using my link script, there are no undefined references, but it seems the link script is being ignored since the output binary does not have the correct entry point.<\/p>\n<p>My (cross-compiler) ld version:<\/p>\n<blockquote>\n<p>&gt; i586-elf-ld &#8211;version<br \/>\nGNU ld (GNU Binutils) 2.20.1.20100303<\/p>\n<\/blockquote>\n<p>My attempts at proving that the &#8216;missing&#8217; symbol is present:<\/p>\n<blockquote>\n<p>&gt; i586-elf-ld -T link.ld -o kernel32.bin kernel_loader.o main.o stdio.o common.o gdt.o gdt.bin -y putch<\/p>\n<pre><code>main.o: reference to putch  \nstdio.o: definition of putch  \nmain.o: In function `main':  \nmain.c:(.text+0x1f): undefined reference to `putch'\n<\/code><\/pre>\n<\/blockquote>\n<p>N.B. (when I produced this output, I was using a filename of gdt.bin for nasm compiled assembler, it is just another .o file, really)<\/p>\n<p>I can see the symbol that is &#8216;missing&#8217; in the appropriate object file:<\/p>\n<blockquote>\n<p>&gt; i586-elf-objdump -ht stdio.o<br \/>\nstdio.o: file format elf32-i386<\/p>\n<pre><code>Sections:\nIdx Name          Size      VMA       LMA       File off  Algn\n  0 .text         000002f9  00000000  00000000  00000034  2**2\n                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE\n  1 .data         0000000c  00000000  00000000  00000330  2**2\n                  CONTENTS, ALLOC, LOAD, DATA\n  2 .bss          00000008  00000000  00000000  0000033c  2**2\n                  ALLOC\n  3 .comment      00000012  00000000  00000000  0000033c  2**0\n                  CONTENTS, READONLY\nSYMBOL TABLE:\n00000000 l    df *ABS*  00000000 stdio.c\n00000000 l    d  .text  00000000 .text\n00000000 l    d  .data  00000000 .data\n00000000 l    d  .bss   00000000 .bss\n00000000 l    d  .comment       00000000 .comment\n00000000 g     F .text  00000016 strlen\n00000016 g     F .text  0000005c scroll\n00000008 g     O .data  00000004 numrows\n00000004 g     O .bss   00000004 ypos\n00000004 g     O .data  00000004 numcols\n00000004       O *COM*  00000004 screen_mem\n00000000         *UND*  00000000 memcpy\n00000000         *UND*  00000000 memsetw\n00000072 g     F .text  0000007d newline\n00000000 g     O .bss   00000004 xpos\n000000ef g     F .text  0000002e writech\n00000000 g     O .data  00000004 colour\n0000011d g     F .text  00000061 cls\n0000017e g     F .text  00000010 init_video\n0000018e g     F .text  00000133 putch\n000002c1 g     F .text  00000037 puts\n000002f8 g     F .text  00000001 set_text_colour\n<\/code><\/pre>\n<\/blockquote>\n<p>And the object file with unresolved reference:<\/p>\n<blockquote>\n<p>&gt; i586-elf-objdump -ht main.o<\/p>\n<pre><code>main.o:     file format elf32-i386\n\nSections:\nIdx Name          Size      VMA       LMA       File off  Algn\n  0 .text         0000007f  00000000  00000000  00000034  2**2\n                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE\n  1 .data         00000000  00000000  00000000  000000b4  2**2\n                  CONTENTS, ALLOC, LOAD, DATA\n  2 .bss          00000000  00000000  00000000  000000b4  2**2\n                  ALLOC\n  3 .rodata.str1.1 00000024  00000000  00000000  000000b4  2**0\n                  CONTENTS, ALLOC, LOAD, READONLY, DATA\n  4 .comment      00000012  00000000  00000000  000000d8  2**0\n                  CONTENTS, READONLY\nSYMBOL TABLE:\n00000000 l    df *ABS*  00000000 main.c\n00000000 l    d  .text  00000000 .text\n00000000 l    d  .data  00000000 .data\n00000000 l    d  .bss   00000000 .bss\n00000000 l    d  .rodata.str1.1 00000000 .rodata.str1.1\n00000000 l    d  .comment       00000000 .comment\n00000000 g     F .text  0000007f main\n00000000         *UND*  00000000 init_video\n00000000         *UND*  00000000 gdt_install\n00000000         *UND*  00000000 putch\n00000000         *UND*  00000000 puts\n00000018       O *COM*  00000001 gdt\n00000006       O *COM*  00000001 gdtp\n<\/code><\/pre>\n<\/blockquote>\n<p>My link script (not sure if it&#8217;s going to be relevant):<\/p>\n<pre><code>OUTPUT_FORMAT(\"binary\")\nENTRY(start)\nphys = 0x00100000;\nSECTIONS\n{\n  .text phys : AT(phys) {\n    code = .;\n    *(.text)\n    *(.rodata*)\n    . = ALIGN(4096);\n  }\n  .data . : AT(data)\n  {\n    data = .;\n    *(.data)\n    . = ALIGN(4096);\n  }\n  .bss . : AT(bss)\n  {\n    bss = .;\n    *(.bss)\n    . = ALIGN(4096);\n  }\n  end = .;\n}\n<\/code><\/pre>\n<p>If I comment out the call to putch in main.c, I instead get undefined references to puts&#8230; if I remove the call to gdt_install, no errors!<\/p>\n<p>gdt_install is in the C file, but gdt_install calls a function which is defined in gdt.asm.<\/p>\n<pre><code>void gdt_install() {\n    \/* ... *\/\n    gdt_reset();\n}\n\n[bits 32]\n[section .text]\nglobal gdt_reset\nextern gdtp\n\ngdt_reset:\n    lgdt [gdtp]\n    mov ax, 0x10      ; 0x10 offset for data segment (sizeof(struct gdt_entry) * 2)\n    mov ds, ax\n    mov es, ax\n    mov fs, ax\n    mov gs, ax\n    mov ss, ax\n    jmp 0x08:gdt_reset2   ; 0x08 offset for code segment (sizeof(struct gdt_entry))\ngdt_reset2:\n    ret              ; ret back to C\n<\/code><\/pre>\n<p>To try and further diagnose the cause, I&#8217;ve been playing around trying to recreate the errors. If I move the gdt_install() function call to a specific place in the source code, I don&#8217;t receive any errors and everything works fine:<\/p>\n<pre><code>int main() {        \n\n    init_video();\n\n    putch('A');\n\n    puts(\"<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:56:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m hoping someone will be able to help troubleshoot what I think is a linker script issue. I&#8217;m encountering a strange problem after adding a call to a new function. Without the function call, my object files link correctly, however, with the new function call added, I get an undefined reference to a symbol from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-899","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}