CFI – Call Frame Information

.cfi_sections section_list
.cfi_sections may be used to specify whether CFI directives should emit .eh_frame section and/or .debug_frame section. If section_list is .eh_frame, .eh_frame is emitted, if section_list is .debug_frame, .debug_frame is emitted. To emit both use .eh_frame, .debug_frame. The default if this directive is not used is .cfi_sections .eh_frame.

On targets that support compact unwinding tables these can be generated by specifying .eh_frame_entry instead of .eh_frame.

Some targets may support an additional name, such as .c6xabi.exidx which is used by the target.

The .cfi_sections directive can be repeated, with the same or different arguments, provided that CFI generation has not yet started. Once CFI generation has started however the section list is fixed and any attempts to redefine it will result in an error.

.cfi_startproc [simple]
.cfi_startproc is used at the beginning of each function that should have an entry in .eh_frame. It initializes some internal data structures. Don’t forget to close the function by .cfi_endproc.

Unless .cfi_startproc is used along with parameter simple it also emits some architecture dependent initial CFI instructions.

.cfi_endproc
.cfi_endproc is used at the end of a function where it closes its unwind entry previously opened by .cfi_startproc, and emits it to .eh_frame.

.cfi_personality encoding [, exp]
.cfi_personality defines personality routine and its encoding. encoding must be a constant determining how the personality should be encoded. If it is 255 (DW_EH_PE_omit), second argument is not present, otherwise second argument should be a constant or a symbol name. When using indirect encodings, the symbol provided should be the location where personality can be loaded from, not the personality routine itself. The default after .cfi_startproc is .cfi_personality 0xff, no personality routine.

.cfi_personality_id id
.cfi_personality_id defines a personality routine by its index as defined in a compact unwinding format. Only valid when generating compact EH frames (i.e. with .cfi_sections eh_frame_entry.

.cfi_fde_data [opcode1 [, …]]
.cfi_fde_data is used to describe the compact unwind opcodes to be used for the current function. These are emitted inline in the .eh_frame_entry section if small enough and there is no LSDA, or in the .gnu.extab section otherwise. Only valid when generating compact EH frames (i.e. with .cfi_sections eh_frame_entry.

.cfi_lsda encoding [, exp]
.cfi_lsda defines LSDA and its encoding. encoding must be a constant determining how the LSDA should be encoded. If it is 255 (DW_EH_PE_omit), the second argument is not present, otherwise the second argument should be a constant or a symbol name. The default after .cfi_startproc is .cfi_lsda 0xff, meaning that no LSDA is present.

.cfi_inline_lsda [align]
.cfi_inline_lsda marks the start of a LSDA data section and switches to the corresponding .gnu.extab section. Must be preceded by a CFI block containing a .cfi_lsda directive. Only valid when generating compact EH frames (i.e. with .cfi_sections eh_frame_entry.

The table header and unwinding opcodes will be generated at this point, so that they are immediately followed by the LSDA data. The symbol referenced by the .cfi_lsda directive should still be defined in case a fallback FDE based encoding is used. The LSDA data is terminated by a section directive.

The optional align argument specifies the alignment required. The alignment is specified as a power of two, as with the .p2align directive.

.cfi_def_cfa register, offset
.cfi_def_cfa defines a rule for computing CFA as: take address from register and add offset to it.

.cfi_def_cfa_register register
.cfi_def_cfa_register modifies a rule for computing CFA. From now on register will be used instead of the old one. Offset remains the same.

.cfi_def_cfa_offset offset
.cfi_def_cfa_offset modifies a rule for computing CFA. Register remains the same, but offset is new. Note that it is the absolute offset that will be added to a defined register to compute CFA address.

.cfi_adjust_cfa_offset offset
Same as .cfi_def_cfa_offset but offset is a relative value that is added/subtracted from the previous offset.

.cfi_offset register, offset
Previous value of register is saved at offset offset from CFA.

.cfi_val_offset register, offset
Previous value of register is CFA + offset.

.cfi_rel_offset register, offset
Previous value of register is saved at offset offset from the current CFA register. This is transformed to .cfi_offset using the known displacement of the CFA register from the CFA. This is often easier to use, because the number will match the code it’s annotating.

.cfi_register register1, register2
Previous value of register1 is saved in register register2.

.cfi_restore register
.cfi_restore says that the rule for register is now the same as it was at the beginning of the function, after all initial instruction added by .cfi_startproc were executed.

.cfi_undefined register
From now on the previous value of register can’t be restored anymore.

.cfi_same_value register
Current value of register is the same like in the previous frame, i.e. no restoration needed.

.cfi_remember_state and .cfi_restore_state
.cfi_remember_state pushes the set of rules for every register onto an implicit stack, while .cfi_restore_state pops them off the stack and places them in the current row. This is useful for situations where you have multiple .cfi_* directives that need to be undone due to the control flow of the program. For example, we could have something like this (assuming the CFA is the value of rbp):

        je label
        popq %rbx
        .cfi_restore %rbx
        popq %r12
        .cfi_restore %r12
        popq %rbp
        .cfi_restore %rbp
        .cfi_def_cfa %rsp, 8
        ret
label:
        /* Do something else */

Here, we want the .cfi directives to affect only the rows corresponding to the instructions before label. This means we’d have to add multiple .cfi directives after label to recreate the original save locations of the registers, as well as setting the CFA back to the value of rbp. This would be clumsy, and result in a larger binary size. Instead, we can write:

        je label
        popq %rbx
        .cfi_remember_state
        .cfi_restore %rbx
        popq %r12
        .cfi_restore %r12
        popq %rbp
        .cfi_restore %rbp
        .cfi_def_cfa %rsp, 8
        ret
label:
        .cfi_restore_state
        /* Do something else */

That way, the rules for the instructions after label will be the same as before the first .cfi_restore without having to use multiple .cfi directives.

.cfi_return_column register
Change return column register, i.e. the return address is either directly in register or can be accessed by rules for register.

.cfi_signal_frame
Mark current function as signal trampoline.

.cfi_window_save
SPARC register window has been saved.

.cfi_escape expression[, …]
Allows the user to add arbitrary bytes to the unwind info. One might use this to add OS-specific CFI opcodes, or generic CFI opcodes that GAS does not yet support.

.cfi_val_encoded_addr register, encoding, label
The current value of register is label. The value of label will be encoded in the output file according to encoding; see the description of .cfi_personality for details on this encoding.

The usefulness of equating a register to a fixed label is probably limited to the return address register. Here, it can be useful to mark a code segment that has only one return address which is reached by a direct branch and no copy of the return address exists in memory or another register.

Referneces
From: https://sourceware.org/binutils/docs-2.35/as/

Catch exception thrown from memory references

By default an exception can only occur during a function call or a throw. If we needs to catch excpetions thrown from trapping instructions, using the `-fnon-call-exceptions`.

Docs:

-fnon-call-exceptions

Generate code that allows trapping instructions to throw exceptions. Note that this requires platform-specific runtime support that does not exist everywhere. Moreover, it only allows trapping instructions to throw exceptions, i.e. memory references or floating point instructions. It does not allow exceptions to be thrown from arbitrary signal handlers such as SIGALRM. 

Example:

#include <iostream>
 
#include <signal.h>
 
using namespace std;
 
static void
sigsegv_handler (int signo)
{
    throw 0;
}
 
int
main (int argc, char *argv[])
{
    int *ptr = nullptr;
    int res;
 
    signal (SIGSEGV, sigsegv_handler);
 
    try {
        res = *ptr;
    } catch (...) {
        cout << "exception" << endl;
    }
 
    return res;
}
g++ -fnon-call-exceptions -o sig sig.cpp

Refernces:
[1] https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html

Load shared libraries with preferred base address

For performance, sometimes we need to adjust the address space layout. The following way allows the shared or executable object to be loaded with preferred base address.

When the dynamic link maps the shared object, the virtual address of segment(that in ELF program header) will be used as the mapping address hints.

Type 1
Link with text-segment starting address.

# Adjust base address to 0x10000 */
gcc -shared ... -Wl,-Ttext-segment=0x10000

Type 2
We can adjust the virtual address of loaded segment by linker scripts.

0x1 Get link script template

gcc -shared -Wl,--verbose > ld.script

Clear the contents before and after the ‘equal sign’.

0x2 Adjust the base address

....
SECTIONS
{
    /* Adjust base address to 0x10000 */
    . = SEGMENT_START("text-segment", 0x10000) + SIZEOF_HEADERS;
    ...
}
....

0x3 Link with script

gcc -shared ... -Wl,-T ld.script

Over!

硬盘盒来电自动启动改造

市面上常见的硬盘盒或硬盘柜几乎都设计了电源按钮开关,并不是外部供电就自动开机的,这也是一种保护措施。但如果我们用这类产品连接家庭服务器或NAS的话,在意外停电恢复后,外置硬盘盒设备就不能访问了,需要手工按一下电源开关。那么,有没有方法可以使这类产品能自动开机呢?方法不仅有,还不止一种哦。

下面,我们以ORICO 9528系列硬盘柜为例,介绍几种方案,其中短接和电容两种方法为来电自动开机方式,而继电器和伺服电机两种方法则可由单片机或各种派来远程控制。


短接
顾名思义,就是将微动电源开关一直按压住,可以使用强力胶带或其它任意有效方法实现。
优势:1. 简单有效且无需拆机。
缺陷:1. 仅部分产品有效。 2. 长期短接是否有风险。 3. 来电不稳定期损坏硬盘。

电容
相对直接短接,还可以串接电容来替换微动开关,在来电时,电容充电此时相当于开关按下,当电容充满时,相当于开关释放。
优势:1. 适用于所有微动开关。2. 无长期短接的不确定风险。
缺陷:1. 需要拆机,影响保修。2. 来电不稳定期损坏硬盘。

继电器
使用程控继电器替换电源开关,在来电后主机启动完成的情况下,由程序控制硬盘柜启动。
优势:1. 适用于所有开关情况。2. 无长期短接的不确定风险。3. 无来电不稳定损坏硬盘。
缺陷:1. 需要拆机,影响保修。

伺服电机
在硬盘柜电源按钮外安装伺服电机,由程序控制电机转动,再由连接在电机转轴上的机械臂按压微动开关。
优势:1. 适用于所有开关情况。2. 无长期短接的不确定风险。3. 无来电不稳定损坏硬盘。4. 无需拆机,不影响保修。
缺陷:暂无。

综合来看,采用伺服电机方法即有安全保障,又无拆机风险,同时成本并不高,大概20元左右。

清单
1. MG90S型舵机 x 1。

2. 母对公杜邦线 x 3。

3. 3M VHB强力双面胶带 x 1 (搜索ETC专用类,这种双面胶有一定的厚度和弹性,在舵机旋臂按压时有缓冲空间)。

安装
1. 剪取与舵机机身等长的两段3M双面胶粘与机身一面。
2. 再将舵机粘贴在硬盘盒微动开关处的合适位置之上。


3. 将舵机GND线连接树莓派GPIO的任意GND引脚,舵机的5V线连接树莓派GPIO的其中一个5V引脚,最后将舵机的信号线连接树莓派的任意具有GPIO功能的引脚。

4. 先不需要着急安装舵机的旋臂,在连接好线路并确认控制程序可工作后,再根据实际的旋转角度一点点调节旋臂,使之在转至最大角度时正好轻触微动开关,防止角度过大损坏设备。

使用
因为NanoPi M4的GPIO信号是3V电平,驱动不了MG90S舵机,正好手上有一块闲置的Rpi2,拿到当舵机控制模块(有点大材小用了,反面都是吃灰的命)。

NanoPi M4使用USB3.0连接硬盘盒,真千兆网口传输数据,再通过GPIO发送开关信号给Rpi2。Rpi2就比较简单了,在GPIO上监听信号事件,事件触发就产生PWM脉冲信号控制舵机旋转,按压开关按钮。

Rpi2自然也不需要跑完整的发行版啦,只要一个内核和initramfs中包含一个静态链接的控制程序作为init进程。

如果仅用树莓派来控制,控制舵机的关键程序如下:

    const double duty_up = 2.0;
    const double duty_down = 4.0;
    int i;
 
    for (i = 0; i < 10; i++) {
        gpio_write (fd, 1);
        usleep (duty_down * 200);
        gpio_write (fd, 0);
        usleep ((100.0 - duty_down) * 200);
    }
 
    for (i = 0; i < 10; i++) {
        gpio_write (fd, 1);
        usleep (duty_up * 200);
        gpio_write (fd, 0);
        usleep ((100.0 - duty_up) * 200);
    }

NanoPi+Rpi组合完整版控制程序:
https://gist.github.com/heiher/1d48924da7f134315e7128aad74ca6e1

Over!