Posts

  • 11th April 2021

Segment tree

In research papers, a segment tree refers to a tree data structure allowing retrieving a list of segments which contain the given point. In competitive programming, the name "segment tree" usually refers to a data structure maintaining an array. According to http://web.ntnu.edu.tw/~algo/Sequence2.html, the data structure originated from Baltic OI 2001: Mars Maps.

…

Read more →
  • 28th March 2021

Compiler driver and cross compilation

Updated 2023-03.

Compiler driver

The gcc program is a compiler driver. It invokes other programs to do the work of compiling (cc1, cc1plus), assembling (GNU as), and linking (collect2). The behavior is controlled by spec strings, which are provided by a plain-text spec file.

…

Read more →
  • 14th March 2021

The dark side of RISC-V linker relaxation

Updated in 2025-07.

This article introduces RISC-V linker relaxation and describes the downside.

Linker optimization/relaxation

Because the linker has a global view and layout information, it can perform some peephole optimizations which are difficult/impossible to do on the compiler side. Generic link-time code sequence transformation is risky, because semantic information is lost and what the linker sees are byte streams. However, if every instruction in the candidate code sequence is associated with one ore more relocations, the ABI and the implementation can assign (additional) semantics to the relocation types and make such transformation safe. This technique is usually called linker optimization or linker relaxation. It seems that the term "linker optimization" is often used when the number of bytes does not change while "linker relaxation" is used when the number of bytes decreases.

…

Read more →
  • 28th February 2021

Linker garbage collection

A program may have a lot of unused code and data. There can be many reasons:

…

Read more →
  • 14th February 2021

All about thread-local storage

Updated in 2026-01.

Thread-local storage (TLS) provides a mechanism allocating distinct objects for different threads. It is the usual implementation for GCC extension __thread, C11 _Thread_local, and C++11 thread_local, which allow the use of the declared name to refer to the entity associated with the current thread. This article will describe thread-local storage on ELF platforms in detail, and touch on other related topics, such as: thread-specific data keys and Windows/macOS TLS.

…

Read more →
  • 31st January 2021

Metadata sections, COMDAT and SHF_LINK_ORDER

Updated in 2025-09.

Modern compilers create metadata sections to support instrumentation, debugging, profiling, and other features. These sections should be retained when their associated code is kept and discarded when their code is removed-but achieving this requires careful linker considerations. This post explores the evolution of solutions, from early workarounds to modern mechanisms like section groups and SHF_LINK_ORDER, examining their trade-offs and real-world applications.

Metadata sections: properties and categories

Metadata sections usually have the following property:

  • Relocations within a metadata section refer to its associated text section or any other auxiliary metadata sections. In many applications, a metadata section doesn't need additional auxiliary sections.
  • Text sections don't have relocations that point to their metadata section. In some cases, a text section might reference its metadata section, but this means that the text section can't be inlined into another.
…

Read more →
  • 24th January 2021

Everything I know about GNU toolchain

As mainly an LLVM person, I occasionally contribute to GNU toolchain projects. This is sometimes for fun, sometimes for investigating why an (usually ancient) feature works in a particular way, sometimes for pushing forward a toolchain feature with the mind of both communities, or sometimes just for getting sense of how things work with mailing list+GNU make.

…

Read more →
  • 18th January 2021

GNU indirect function

Updated in 2026-07.

GNU indirect function (ifunc) is a mechanism making a direct function call resolve to an implementation picked by a resolver. It is mainly used in glibc but has adoption in FreeBSD.

…

Read more →
  • 9th January 2021
  • 31st December 2020

2020年总结

2017年10月第一次给LLVM提交patch,到2020年底已经贡献三年多了。今年~1000 commits,灌水很多,挑出一些值得一提的。

…

Read more →