jackhab
jackhab

Reputation: 17698

Overwriting library file causes segmentation fault

After I have the executable file running I overwrite its .so library file with a new version and this causes the executable to die with segmentation fault. I thought the library file is being accessed only when ELF file is loaded. Am I wrong?

Upvotes: 4

Views: 1692

Answers (2)

MarkR
MarkR

Reputation: 63538

Like caf said, it's not a good idea to overwrite an executable while it's running.

Instead, write the new files as a temporary file in the same directory, then rename it atomically with rename(). This is what installers typically do.

Upvotes: 2

caf
caf

Reputation: 239011

The library file is mapped into the memory when it is loaded (usually, when the executable is loaded - but libraries can also be loaded later with dlopen()). The actual pages of the file are then demand-loaded as required.

Overwriting the file will cause pages from the file mapped MAP_SHARED (which is most of them) to be updated with the new contents. This is what causes the segmentation faults. Don't do that - instead, remove the existing .so file, then write the new one in its place.

Upvotes: 12

Related Questions