构建一个.so,这也是一个可执行 [英] building a .so that is also an executable

查看:146
本文介绍了构建一个.so,这也是一个可执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以每个人大概都知道的glibc的 /lib/libc.so.6 可以像在哪些情况下它打印其版本信息并退出一个正常的可执行shell执行。这是通过在所述的.so限定的入口点进行。对于某些情况下,它可能是有趣的,也将此信息用于其他项目。不幸的是,低级别的切入点,可以通过LD的-e选项设置是有点太低级:动态加载程序不可用,所以你不能调用任何适当的库函数。的glibc为此通过实施这个切入点赤裸裸的系统调用写()系统调用。

So everyone probably knows that glibc's /lib/libc.so.6 can be executed in the shell like a normal executable in which cases it prints its version information and exits. This is done via defining an entry point in the .so. For some cases it could be interesting to use this for other projects too. Unfortunately, the low-level entry point you can set by ld's -e option is a bit too low-level: the dynamic loader is not available so you cannot call any proper library functions. glibc for this reason implements the write() system call via a naked system call in this entry point.

我现在的问题是,任何人都可以想到的一个很好的途径之一如何引导从该入口点一个完整的动态连接器这样就可以从其他的.so的访问功能?

My question now is, can anyone think of a nice way how one could bootstrap a full dynamic linker from that entry point so that one could access functions from other .so's?

推荐答案

-pie 选项出现,给你一切你想要建立你的共享库:

Building your shared library with -pie option appears to give you everything you want:

/* pie.c */
#include <stdio.h>
int foo()
{
  printf("in %s %s:%d\n", __func__, __FILE__, __LINE__);
  return 42; 
}
int main() 
{ 
  printf("in %s %s:%d\n", __func__, __FILE__, __LINE__);
  return foo(); 
}


/* main.c */
#include <stdio.h>

extern int foo(void);
int main() 
{ 
  printf("in %s %s:%d\n", __func__, __FILE__, __LINE__);
  return foo(); 
}


$ gcc -fPIC -pie -o pie.so pie.c -Wl,-E
$ gcc main.c ./pie.so


$ ./pie.so
in main pie.c:9
in foo pie.c:4
$ ./a.out
in main main.c:6
in foo pie.c:4
$

P.S。 glibc的工具写(3)通过系统调用,因为它没有其他地方调用(这是的最低的已级)。这有没有关系能够执行 libc.so.6的

P.S. glibc implements write(3) via system call because it doesn't have anywhere else to call (it is the lowest level already). This has nothing to do with being able to execute libc.so.6.

这篇关于构建一个.so,这也是一个可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆