"Reflection" in the ACE Service Configurator framework and C language

zhaozj2021-02-16  63

The ACE service configuration framework is a very powerful framework that uses it to make the application to be selected according to the appropriate configuration file, and the application can reconfigure its service without recompiling or restarting the application itself. For applications in telecommunications, the framework function is indeed great. However, for a static type of service, that is, the corresponding function is static compiled into the app, and the ACE generally uses ACE_STATIC_SVC_REGIRE and ACE_STATIC_SVC_REGISTER in the main program to register the static service to the ACE_SERVICE_REPOSITORY, which causes main programs and services. There is still a certain coupling between. Of course, there is no problem for static services. However, this is much less, I feel uncomfortable. I am an extremist ^ _ ^, I want my main program to start static service through the configuration file, and there is no coupling on the code on the code on the code. !

In Java, you can get the class itself through a class name. I am not familiar with java ^ _ ^, the reflection in Java is probably this. In order to meet the above requirements, the main program starts a static service through the configuration file, that is, we have to get the corresponding function according to the name of the function (for the dynamic load-loaded service through the shared library, we can pass Dlopen, Dlsym and other functions, It is easy to export a function itself from a function name itself. The underlying implementation is also in this way, to dynamically load the service), so I wrote the "reflection" ^ _ ^ ^ ^ ^, written in the title. In C and C , we have no way to do this from the perspective of language.

As the saying goes: there is no road to the sky. There is no Can't do it (c is able to do it, but many times I can't do it, because my horizontal is still very bad ^ _ ^). Here we can get the function itself from the function name by parsing the ELF file format, the code is just a simple presentation):

File name: Parse.c

#include #include #include #include #include #include

Void Test (INT I) {Printf ("OH, Come on Baby / N"); Printf ("THE I IS% D / N", I);}

INT Main (int Argc, char ** argv) {ELF * ELF = NULL; ELF_SCN * SCN = NULL; Gelf_SHDR SHDR; ELF_DATA * DATA = NULL; INT FD, II, COUNT

Void (* func) (int);

ELF_VERSION (EV_CURRENT);

FD = Open (argv [1], o_rdonly); ELF = Elf_begin (fd, elf_c_read, null);

While ((SCN = Elf_NextScn (ELF, SCN))! = null) {geelf_getshdr (SCN, & SHDR); if (SHDR.SH_TYPE == SHT_SYMTAB) {/ * Found A Symbol Table, Go Print It. * / Break; } DATA = Elf_getdata (SCN, NULL); count = shdr.sh_size / shdr.sh_entsize;

For (ii = 0; II

Gelf_getsym (DATA, II, & SYM); if (! strcmp ("test", Elf_strptr (ELF, SHDR.SH_LINK, SYM.ST_NAME))) {FUNC = (VOID (*) (INT) (SYM) .st_value & 0xffffffffff); // Get the function address FUNC (7) corresponding to the function name;}}

ELF_END (ELF); Close (FD);

Compilation: GCC -O Parse Parse.c-delf

Run: Parse Parse

Through the above means, we get the corresponding function from the name of the function. This code compiles running on Solaris. On the Internet, the program HACK is analyzed by analyzing the ELF. I simply analyze the ELF, which is used for the right way, and it is also known as a famous door. (Don't still egg ^ _ ^)

In fact, a lot of things is a kind of thinking. When I didn't see the ACE, I never thought of the selection started by the configuration file. When I saw ACE, I said: "Hey, the original program can be like this", and through your own divergent thinking, I can further consider something. Hey. . . It seems that there is still a lot of things that you have to learn, you still have a tender cucumber ^ _ ^.

转载请注明原文地址:https://www.9cbs.com/read-22313.html

New Post(0)