gcc

gcc
Exisfargcc
使用指定的gcc
Q:直接在命令行中设置环境变量并运行脚本 or 在脚本内部设置环境变量时?
A:一般直接在命令行中设置环境变量并运行脚本。如果这样,CC 和 CXX 环境变量在执行脚本前已经被设置,并且这些变量会在脚本执行期间生效。相比之下,在脚本内部设置环境变量时,这些变量只在脚本的执行上下文中有效,并不会影响调用该脚本的父 shell 会话。如果脚本中有子进程或其他命令需要使用这些环境变量,它们可能不能正确继承这些变量。
export CC=... |
编译自己的glibc gcc clang
- gcc特定版本Linux源码编译安装 (以gcc-8.5.0为例)
- How to compile my own glibc C standard library from source and use it?
- 如何使用新的glibc来编译自己的程序
编译glibc报错
Just add the --disable-werror to your configure setup:
$ # if you are in glibc-build |
from GNU manual:
By default, the GNU C Library is built with -Werror. If you wish to build without this option (for example, if building with a newer version of GCC than this version of the GNU C Library was tested with, so new warnings cause the build with -Werror to fail), you can configure with --disable-werror.
-PIE and -PIC
如果没有位置无关可执行文件(PIE)和位置无关代码(PIC),可执行文件和共享库将具有以下特点:
- 固定地址加载:可执行文件和共享库将在内存中的固定地址加载。这意味着它们的代码和数据段在编译时就已经确定了内存地址。
- 缺乏ASLR保护:没有PIE和PIC的可执行文件和共享库无法利用地址空间布局随机化(ASLR)技术,这使得它们更容易受到攻击者的攻击,因为内存地址是可预测的。
- 重定位开销:没有PIC的共享库在加载时需要进行重定位,这会增加加载时间和内存开销。
- 内存使用效率低:没有PIC的共享库在多个进程中加载时,每个进程都需要一份独立的副本,无法共享代码段,从而增加了内存使用。
- 兼容性问题:没有PIE和PIC的可执行文件和共享库在不同系统和环境中的兼容性较差,因为它们依赖于固定的内存地址。
总的来说,没有PIE和PIC的可执行文件和共享库在安全性、内存使用效率和兼容性方面都存在一定的劣势。现代操作系统通常默认启用PIE和PIC,以提高系统的安全性和效率。
位置无关可执行文件(PIE)的主要好处包括:
- 增强安全性:PIE 允许可执行文件在内存中的不同位置加载,从而使得攻击者更难预测内存地址,增加了攻击难度。这种技术称为地址空间布局随机化(ASLR)。
- 共享库的灵活性:PIE 使得可执行文件可以像共享库一样在内存中的任何位置加载,从而提高了内存使用的灵活性和效率。
- 减少重定位开销:PIE 可以减少在加载时的重定位开销,因为它们可以直接在加载时进行重定位,而不是在运行时。
- 兼容性:PIE 提高了可执行文件在不同系统和环境中的兼容性,因为它们不依赖于固定的内存地址。
总的来说,PIE 提供了更高的安全性和灵活性,特别是在现代操作系统中,ASLR 已成为一种常见的安全措施。
program compiled by riscv64-elf-gcc and riscv64-linux-gnu-gcc
No, programs compiled with riscv64-linux-gnu-gcc
cannot be directly executed on an x86-64 Linux system either. Let me clarify:
1. Programs Compiled by riscv64-elf-gcc
- Target: These are bare-metal programs targeting RISC-V embedded systems. They do not rely on an operating system or Linux kernel.
- Execution:
- Must be run on a RISC-V processor or system.
- Can be emulated using tools like QEMU with appropriate RISC-V support.
2. Programs Compiled by riscv64-linux-gnu-gcc
- Target: These are programs for a Linux system running on RISC-V architecture. They are linked against the RISC-V Linux system libraries.
- Execution:
- Must be run on a RISC-V Linux system with compatible libraries and kernel.
- Can also be emulated using QEMU, but only in a full system or Linux-user emulation mode configured for RISC-V.
Why Can’t riscv64-linux-gnu-gcc
Programs Run on x86-64 Linux?
The issue is the same: instruction set architecture mismatch. Programs compiled by riscv64-linux-gnu-gcc
contain RISC-V machine code, which is incompatible with x86-64 processors. Emulators like QEMU translate RISC-V instructions into x86-64 instructions to allow execution on x86-64 hardware.
Summary:
- Both
riscv64-elf-gcc
andriscv64-linux-gnu-gcc
compile programs for the RISC-V architecture, and the resulting binaries are not directly executable on x86-64 systems. - The difference is:
riscv64-elf-gcc
: Targets bare-metal RISC-V systems (no OS).riscv64-linux-gnu-gcc
: Targets RISC-V Linux systems.
- To run on x86-64, emulation (e.g., with QEMU) is required for either case.