使用nasm和gcc生成无格式的二进制文件

news/2024/6/29 12:16:14 标签: gcc, ubuntu, 汇编, 编译器, linux, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

前两天看到一篇文章࿰c;color:#333333">使用color:#333333">Ccolor:#333333">class="tags" href="/tags/BianYiQi.html" title=编译器>编译器创建无格式的二进制文件(color:#333333">i386+color:#333333">)(color:#333333">1-6color:#333333">)(译)

  http://xieyubo.spaces.live.com/blog/cns!55B39819C9DA4A2!282.entry

 

class="cjk" style="margin-right:0px; margin-left:0px; margin-top:0cm; margin-bottom:0.13cm; font-size:24px; line-height:0.58cm">   提供下面两种方法编译生成一个没有格式的二进制文件:

ckquote style="border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; color:rgb(51,51,51); margin-left:40px; padding-left:0px; padding-right:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-style:italic; font-size:11.5px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; margin-right:0px; border-width:initial; border-color:initial">

1. 

  class="tags" href="/tags/GCC.html" title=gcc>gcc -c test.c 

  ld -o test -Ttext 0x0 -e main test.o

  objcopy -R .note -R .comment -S -O binary test test.bin

class="cjk" style="margin-top:0cm; margin-bottom:0.13cm; font-size:18px; line-height:0.58cm"> 2.

  class="tags" href="/tags/GCC.html" title=gcc>gcc -c test.c 

ckquote> ckquote style="border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; color:rgb(51,51,51); margin-left:40px; padding-left:0px; padding-right:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-style:italic; font-size:11.5px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; margin-right:0px; border-width:initial; border-color:initial">

  ld test.o -o test.bin -Ttext 0x0 -e main -oformat binary

ckquote>

 

  现在假设我们有一个段使用class="tags" href="/tags/HuiBian.html" title=汇编>汇编编写的程序࿰c;而我们又需要将它加入我们最后生成的文件中࿰c;我们可以使用下面的方法:

ckquote style="border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; color:rgb(51,51,51); margin-left:40px; padding-left:0px; padding-right:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; font-style:italic; font-size:11.5px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; margin-right:0px; border-width:initial; border-color:initial">

  nasm -f elf other.asm -o other.o

  class="tags" href="/tags/GCC.html" title=gcc>gcc -c test.c 

  ld other.o main.o  -o kernal.bin -Ttext 0x0 -e _mian  --oformat binary 

ckquote>

  目前class="tags" href="/tags/LINUX.html" title=linux>linux大多都是elf格式目标文件࿰c;所以我们首先将class="tags" href="/tags/HuiBian.html" title=汇编>汇编程序编译为elf的目标文件࿰c;然后使用ld将多个目标文件链接到一起即可。

  使用GNU ld (GNU Binutils for Ubuntu) 2.20࿰c; class="tags" href="/tags/GCC.html" title=gcc>gcc (Ubuntu 4.4.1-4class="tags" href="/tags/UBUNTU.html" title=ubuntu>ubuntu8) 4.4.1 ࿰c; NASM version 2.05.01   

cle>

http://www.niftyadmin.cn/n/823028.html

相关文章

HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!

前言Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据。本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始之前我觉得有必要谈谈 HashMap,没有它就不会有后面的 ConcurrentHashMap。HashMap众所周知 …

HDU5668 Circle 非互质中国剩余定理

分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然不要忘了判无解的情况. 有非常多选手似乎都是一眼标算然…

isKindOfClass,isMemberOfClass使用备忘

isMemberOfClass 判断是否是属于这类的实例isKindOfClass 判断是否是这个类或者这个类的子类的实例 if ([teacher isKindOfClass:[Teacher class]]) { NSLog("teacher 是 Teacher类或Teacher的子类"); }if ([teacher isMemberOfClass:[Person class]]) { NSLog(&q…

技术负责人所需的四个核心能力,你具备几个?

「技术负责人」这一称呼其实比较泛了。往大了讲,可以指 CTO、技术VP、技术总监,往小了讲,可以指 小组Leader、技术主管、架构师 等。这些不同岗位的「技术负责人」在工作中会处理着各不相同的问题,因此对他能力要求的侧重点肯定也…

安卓减小apk的体积,整体优化代码

百度到的方法都太普通,还是通过as自带的功能来实现吧,有两种方法。 1、首先生成apk文件,点击Build->Build Apk(s),然后再点击Build->Analyze Apk...会生成如下界面。可以直观的看出到底是那一部分占的…

写了300多篇文章了,说说我为什么坚持写博客

作者简介:骏马金龙(网名),钟情于IT世界里的各种原理和实现机制,强迫症重症患者。爱研究、爱翻译、爱分享。博客专栏: https://www.cnblogs.com/f-ck-need-u原来,我已经写了300多篇博客了。现在想…

编译原理(二)

一、简单表达式的翻译器 通过上一篇知道,预测语法分析不能处理左递归的文法(比如对stmt分析,则遇到多个以stmt开头的产生式,这使得向前看符号无法匹配一个分支,而右递归则是可以使用预测分析法处理)。 1. 抽…

Android调用相机拍照录视频录音以及存储,7.0以上及以下都可使用。

在开头,先说一下在imageVIew中如何显示文件中存储的视频的第一帧的图片。很简单,几行代码就搞定。 MediaMetadataRetriever media new MediaMetadataRetriever();media.setDataSource(user.getPath());Bitmap bitmap media.getFrameAtTime(); 下来&a…