简体   繁体   中英

why does an object file(.o files in linux) look gibberish when opened with vim text editor?

what are the symbols that we see when we open object files with text editor, do they(symbols like @,..) have any meaning? Since object file is a binary file, we expect to see 1's and 0's but why do we see those symbols?

Because a binary file contains binary data, not the ASCII data that your text editor is expecting to see. It is up to the editor how it will display "non printable" characters.

On Linux you can use od to dump a file in other formats (octal, hex, binary) if you really want to see the 0s and 1s.

The symbols you're seeing are C0 Codes in caret notation and they represent non-printing ASCII characters.

.o files don't contain human-readable text, so they look like nonsense when Vim tries to read them as ASCII.

If you'd like to see these characters as their component bytes (like <00> instead of ^@ ), try :set display+=uhex (see also :help display ).

Since object file is a binary file, we expect to see 1's and 0's

Your expectation is incorrect . If you want to see 0s and 1s, you need to use a tool that displays 0s and 1s.

I can find no standard utility that would do this, and nobody ever really wants to see 0s and 1s, because such a display would be very verbose. Programmers prefer hexadecimal display, which is much more compact. For example, the hex value 0xfd can be expressed as the following sequence of 0s and 1s: 1111 1101 .

Note 1: It's exactly the same sequence of 0s and 1s, expressed in different way.

Note 2: You can see that expressing numbers as bits quickly gets out of hand: 0xfffffffd is 1111 1111 1111 1111 1111 1111 1111 1101

but why do we see those symbols?

The vim editor interprets the sequence of 0s and 1s as ASCII characters. For example, ASCII code for 'A' is 0x41 , or the following sequence of 0s and 1s: 0100 0001 . Full ASCII table here .

To understand this better, create a file foo.txt in vim that has the following lines:

AAAAaaaa
BBBBbbbb

Then run this file through:

od -c foo.txt
od -b foo.txt
od -tx1 foo.txt
cat foo.txt

Note that the 0s and 1s contained in the file do not change, but each of the above commands interprets these 0s and 1s differently.

Back to your binary file.

All files contain a sequence of bytes. These bytes can be interpreted differently by different tools.

When people say "binary file" vs. "text file", what they usually mean is that the latter contains only bytes that have printable ASCII characters associated with them, and the former contains some bytes that have non -printable ASCII characters (it is very common for a binary file to also contain bytes that correspond to printable ASCII . There is a strings utility which can extract such printable sequences of bytes from "binary files" and interpret them as "text". Note that sometimes that interpretation makes a "mistake", and interprets binary data as a printable string, as can be seen here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM