简体   繁体   中英

du and size report different values for object files

While compiling a project I notice that du and size command outputs don't add up:

> du -sh X.o
490K X.o

> size X.o
  text    data     bss     dec     hex filename
  2128       0      12    2140     85c X.o

Why is the disk space taken by the object file different from the sum of the text data and bss segments of the file? What am I missing here?

The size command shows you how much the code and data will take up during execution. The object file consists of much more than that of course.

It begins with the overhead of the file format itself, which would have to contain at least the information that size uses to find out how big each part will be in memory. Then there's symbol tables, debugging information and who knows what (depends on compiler and object file format).

You can get more comprehensive information with objdump -h (or objdump -x to see just how many relocation records there are) which still doesn't cover the overhead, but shows how much actual content there is.

du displays the size of the file which resides on file system Vs. size which is actual size in bytes.

Reason behind why du has huge size - file systems usually made up of blocks on which files doesn't fit into these blocks exactly causing this difference. For example, if the file size is 4096 Bytes, size displays 4096 simliar to du but when file size is 5000 Bytes, size displays 5000 Bytes but du displays 8192.

This is referred as slack space .

Note: Above assuming file system allocation in units of 4096 Bytes.

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