linux 正在运行的程序不能拷贝_如何实现在Windows上运行Linux程序,附示例代码

初步了解ELF格式

首先让我们先了解什么是原生Linux程序, 以下说明摘自维基百科

In computing, the Executable and Linkable Format (ELF, formerly named Extensible Linking Format), is a common standard file format for executable files, object code, shared libraries, and core dumps. First published in the specification for the application binary interface (ABI) of the Unix operating system version named System V Release 4 (SVR4),[2] and later in the Tool Interface Standard,[1] it was quickly accepted among different vendors of Unix systems. In 1999, it was chosen as the standard binary file format for Unix and Unix-like systems on x86 processors by the 86open project.By design, ELF is flexible, extensible, and cross-platform, not bound to any given central processing unit (CPU) or instruction set architecture. This has allowed it to be adopted by many different operating systems on many different hardware platforms.

Linux的可执行文件格式采用了ELF格式, 而Windows采用了PE格式, 也就是我们经常使用的exe文件的格式.

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

ELF格式的结构如下

50c80985882b583a8b530fec08d9bd39.png

大致上可以分为这些部分

  • ELF头,在文件的最开头,储存了类型和版本等信息
  • 程序头, 供程序运行时解释器(interpreter)使用
  • 节头, 供程序编译时链接器(linker)使用, 运行时不需要读节头
  • 节内容, 不同的节作用都不一样
  • .text 代码节,保存了主要的程序代码
  • .rodata 保存了只读的数据,例如字符串(const char*)
  • .data 保存了可读写的数据,例如全局变量
  • 还有其他各种各样的节

让我们来实际看一下Linux可执行程序的样子

以下的编译环境是Ubuntu 16.04 x64 + gcc 5.4.0, 编译环境不一样可能会得出不同的结果

首先创建hello.c,写入以下的代码

#include int max(int x, int y) { return x > y ? x : y;}int main() { printf("max is %d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值