-1

Stephen Kitt wrote in https://unix.stackexchange.com/a/448943

any ELF object with an entry point can be an executable, regardless of its other properties.

and ld.so is both an executable (because of having an entry point) and a shared library.

What is the necessary and sufficient condition for an ELF object file to be a shared library?

What is the necessary and sufficient condition for an ELF object file to be a relocatable object file?

The following quote says that a shared library is a relocatable object file.

Is an executable file also a relocatable object file?

Is an executable a shared library?

CSAPP says

7.3 Object Files

Object files come in three forms:

Relocatable object file. Contains binary code and data in a form that can be combined with other relocatable object files at compile time to create an executable object file.

Executable object file. Contains binary code and data in a form that can be copied directly into memory and executed.

Shared object file. A special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run time.

Tim
  • 101,790

1 Answers1

4

What is the necessary and sufficient condition for an ELF object file to be a shared library?

To be of ELF type DYN. To be useful, it should also export the symbols constituting the library’s ABI.

What is the necessary and sufficient condition for an ELF object file to be a relocatable object file?

To be of ELF type REL. This implies that its code is relocatable, and that it contains a relocation table.

Is an executable file also a relocatable object file?

Executables can be relocatable, but they are not REL files.

Is an executable a shared library?

They can be, but most aren’t. Note however that many executables are of ELF type DYN, even though they aren’t intended to be used as shared libraries. This indicates that they are position-independent executables (see DT_FLAGS_1).

Stephen Kitt
  • 434,908
  • Thanks. "many executables are of ELF type DYN, even though they aren’t usable as shared libraries. This indicated that they are position-independent executables." (1) What is the necessary and sufficient condition for an ELF object file to be an executable? To be of ELF type ELF_EXEC, to have an entry point, and/or to have .interp section? (2) "they aren’t usable as shared libraries". Then why are their ELF types ELF_DYN? (3) In CSAPP, position-indepndent is introduced for shared libraries, (not mentioned for executables) in 7.12 Position-Independent Code (PIC). – Tim Sep 28 '20 at 01:23
  • (1) See my answer to your other comment asking the same thing. (2) I’ve updated my answer to clarify that they aren’t intended to be used as shared libraries (although they can, technically). (3) I imagine CSAPP’s authors have their reasons. PIE came later than PIC (which also helps answer the end of (2)). – Stephen Kitt Sep 28 '20 at 05:29