4

I am building a shared library on linux using gcc. I get no compile or link errors but my shared object always has the execution bit set (though readelf indicates its a shared object). I am just doing this:

> echo "int f() {return 1;}" > a.cpp
> gcc -c a.cpp
> gcc -shared -o liba.so a.o
> ls -l liba.so
-rwxr-xr-x 1 me me 6652 2011-06-09 17:05 liba.so

Why would a shared object have the execute bit set?

samwise
  • 181
  • 2
  • 10

2 Answers2

5

Because generally you can execute shared libraries. Most shared libraries will crash when you execute them, but you can execute them.

Some libraries have an entry point defined (-Wl,-e,the_name_of_your_entry_point, the main function in standard C programs) defined, then you can execute them without a crash.

ckruse
  • 9,642
  • 1
  • 25
  • 25
1

HP-UX requires that shared libraries be executable, for example. If they're not, the memory map by the dynamic loader fails with permission denied. And presumably it doesn't hurt to have an x bit on other platforms, so GCC takes the safe route and sets the x bit.