Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Could self-modifying code be used to speed up function calling in dynamically linked libraries?

As far as I understand it, ELF shared libraries use a kind of indirect jump table (the procedure linkage table, or PLT) to enable lazy binding of library functions. The purpose seems to be to avoid having to modify the table in the code segment while still enabling lazy resolution of function positions at the first call.

Wouldn't it be faster to dynamically create the code for that table at load time, or possibly even at the first function call?

Is it to enable sharing of the code segment between processes as much as possible (a dynamic table would be private to a process)? Is it for security reasons (writable code shouldn't be executable - but JITs do that all the time, and the write permission could be added and removed by the loader before actually starting the program)?

Or is it a combination of those, and the small performance gain per function call just wouldn't be worth the effort?

share|improve this question

1 Answer 1

I suppose we're talking about x86 architecture.

You cannot have Self-Modifying Code in protected mode, which is used by most UNIX-based operating systems (and not only) that I'm aware of, because the code segments are always read-only. A loader does not control that -it is something that is being handled by the memory management subsystem of the kernel.

But even if you could "create the code for that table at load time" as you say, it would defy the whole purpose of shared libraries. This way, each process would have a "private" copy of the library's functions in its address space, effectively increasing its memory footprint -one of the reasons that shared libraries were created, was to address this issue.

The whole process you that you describe is quite complex, and it would cost more processing cycles than the PLT method that is used nowadays, and probably would introduce more, new & interesting security issues.

share|improve this answer
1  
You can use the mprotect(2) system call to make .text segment pages writeable and executable. –  Bruce Ediger Nov 15 '12 at 22:30
    
That is correct, sir. Probably it would work decently on the average UNIX-based system, but the whole linking process would break as soon as someone tried to harden the system with e.g PaX, which enforces restrictions on mprotect(2). –  dkaragasidis Nov 16 '12 at 11:36
    
(It seems that I missed to mention @BruceEdiger in my previous comment) –  dkaragasidis Nov 16 '12 at 11:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.