Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker scripts: strategies for debugging?

I'm trying to debug a linker problem that I have, when writing a kernel.

The issue is that I have a variable SCAN_CODE_MAPPING that I'm not able to use -- it appears to be empty or something. I can fix this by changing the way I link my program, but I don't know why.

When I look inside the generated binary file using objdump, the data for the variable is definitely there, so there's just something broken with the reference to it.

Here's a gist with both of the linker scripts and the part of the symbol table that's different between the two files.

What confuses me is that both of the symbol tables have all the same symbols, they're all the same length, and they appear to contain the right data. The only difference that I can see is that they're not in the same order.

So far I've tried

  • inspecting the SCAN_CODE_MAPPING memory location to make sure it has the data I expect and hasn't been zeroed out
  • checking that all the symbols are the same
  • checking that all the symbol contents are the same length
  • looking at .data.rel.ro.local to make sure it has the address of the data

One possible clue is this warning:

warning: uninitialized space declared in non-BSS section `.text': zeroing

which I get in both the broken and the correct case.

What should I try next?

like image 204
jvns Avatar asked Dec 11 '13 18:12

jvns


People also ask

What are linker scripts?

A linker script is a file that tells the linker which sections to include in the output file, as well as which order to put them in, what type of file is to be produced, and what is to be the address of the first instruction.

How do you write a script linker?

You write a linker script as a series of commands. Each command is either a keyword, possibly followed by arguments, or an assignment to a symbol. You may separate commands using semicolons. Whitespace is generally ignored.

What is align in linker file?

The /FILEALIGN linker option lets you specify the alignment of sections written to your output file as a multiple of an specified size.

Where is the default linker script?

The linker always uses a linker script. If you do not supply one yourself, the linker will use a default script that is compiled into the linker executable. You can use the -verbose command line option to display the default linker script.


1 Answers

The problem here turned out to be that I was writing an OS, and only 12k of it was being loaded instead of the whole thing. So the linker script was actually working fine.

The main tools I used to understand binaries were:

  • nm
  • objdump
  • readelf
like image 58
jvns Avatar answered Sep 28 '22 09:09

jvns