Am urmat pasii temei si mi-am construit un libtip.so care sa suprascrie
read / write, pentru statistici. Din obisnuinta, am inceput sa scriu in
C++, si am urmat obiceiul de a exporta simboluri:<br><br><span style="font-family: courier new,monospace;">#ifdef __cplusplus</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
extern "C"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
#endif</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        // The method called when the library is loaded</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        void on_load( void ) __attribute__((constructor));</span><br style="font-family: courier new,monospace;">        <br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        // The method called when the library is unloaded</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        void on_unload( void ) __attribute__((destructor));</span><br style="font-family: courier new,monospace;">
        <br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        // The overwritten read method</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        ssize_t read(int fd, void *buf, size_t count);</span><br style="font-family: courier new,monospace;">        <br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        // The overwritten write method
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        ssize_t write(int fd, const void *buf, size_t count);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#ifdef __cplusplus</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br><br>Le-am suprascris lasandu-le goale, intorcand doar niste valori stub.<br>Am obtinut obj-urile cu urmatoarea sectiune din makefile:<br><br><span style="font-family: courier new,monospace;">
%.o: %.cpp</span><br style="font-family: courier new,monospace;">
<div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">        @$(CXX) -Wall -g -fPIC -c -o $@ $<</span><br>
</div>
<br>Am obtinut libraria cu urmatoarea sectiune din makefile:<br><span style="font-family: courier new,monospace;"><br>
$(OUTLIB): $(OBJS)</span><br style="font-family: courier new,monospace;">
<div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">$(CXX) -shared -Wl,-soname,libtip.so -ldl -o libtip.so $(OBJS)<br>
</span></div>
<br>
Am verificat existenta simbolurilor exportate, cu ajutorul nm. Totul este in regula.<br>
<br>
Problema e ca libraria mea nu functioneaza corect. Am incercat:<br>
<br>
<span style="font-family: courier new,monospace;">LD_LIBRARY_PATH=. LD_PRELOAD=libtip.so ls</span><br>
<br>
Imi afiseaza continutul directorului la stdout. Am mai consultat niste
pareri de la cei batrani si mi s-a spus ca e posibil sa fie o problema
de la faptul ca libraria e compilata in C++, si ca sa incerc cu un
program scris in C++. Asa ca am facut clasicul "Hello world!" in
main.cpp ( cu printf din stdio.h ) si l-am compilat cu g++ si am
incercat:<br>
<br>
<span style="font-family: courier new,monospace;"> LD_LIBRARY_PATH=. LD_PRELOAD=libtip.so ./main</span><br>
<br>
Care din nou nu a functionat corect, afisandu-mi mesajul. M-am gandit
sa purific calea catre apelul write ( pentru ca totusi printf apeleaza
write ) si am scris direct write pe STDOUT_FILENO. Am rulat din nou si
a functionat.<br>
<br>
Problemele sunt, deci, urmatoarele:<br>
<ol>
<li>De ce nu pot sa folosesc libraria mea compilata in C++ decat
pentru programe C++ (eventual ce flaguri am / n-am pus la compilare)</li>
<li>De ce, totusi, pentru programele C++, daca apelez printf in
programul de test nu se intercepteaza apelul de catre libraria mea
(main.cpp a fost compilat cu "<span style="font-family: courier new,monospace;">g++ -o main main.cpp</span>"<br>
</li>
</ol>
Am sa scriu libraria in C, dar totusi as vrea niste explicatii pentru ceea ce se intampla.<br>
Va multumesc!<br>
<br>
-- <br>Regards,<br>Alexandru Trifan<br>