ChatGPT解决这个技术问题 Extra ChatGPT

What are .S files?

I've seen .S files in various projects such as in the Linux Kernel, FreeBSD kernel, or in some other projects. What do these files do and why can't we have .c files instead ?


l
lukecampbell

.S files are source code files written in assembly. Assembly is an extremely low-level form of programming. The files contain assembly instructions to the processor in sequential order and are typically compiled based on a selected architecture. Examples of such files are often seen in the linux kernel for specific architectures, e.g. x86, sparc, ARM, etc.

For more information about assembly language:

X86 Assembly/GAS syntax

x86 Instruction list

TLDP Linux Assembly Howto

Example in the Linux kernel: arch/x86/net/bpf_jit.S


Why isn't it called .asm?
Because someone preferred .s.
@Liga: IDK why .s / .S was originally chosen by Unix assemblers, but these days on modern x86 it's normal to use .asm for NASM/YASM/FASM/MASM/JWASM/etc source files (using directives like dd, and .S for GAS/clang asm source files (using directives like .long, and defaulting to AT&T syntax). I think some people do use .asm for GAS source files, though, especially for other architectures like ARM, perhaps especially for embedded development. (Certainly for non-GAS ARM assemblers like Keil's that use different syntax.)
D
DrBeco

.S files are usually assembly language.

Something nobody mentioned is, why capital S?

.S (capital S) stands for assembly code that must still pass through a pre-processor. That means it can have #include and #define among other macros. It may also be written as .sx.

.s (lowercase s) is pure assembly code that can be compiled into an object.

Why not use .c? Well, being an operating system, it is impossible to write everything in C. Actually, that would be ideal, and C language itself has a background history linked to help creating operating systems and diminish the amount of assembly needed to code it. But many low-level operations are too dependant of the machine.

Here a nice example of a memory copy routine for the Linux boot that uses #include <linux/linkage.h>.


Yes, this answer is very important. I just encountered the ".s" issue recently. The code is a copy from other projects, I just changed .S to .s. I have to change the #define SOME_CONST 1 to SOME_CONST = 1 and put it into the .s file. It puzzled me a lot. I inspected the compilation flags, no clue...then I was guessing probably the suffix matters? Then a search hit this link and answer :)
A
Aryan Beezadhur

.S files are assembly files.

Why .S & why not .c files?

Its because machine dependent stuff and early initialization such as setting up cache and memory can only be done with assembly level instructions such as I/O instructions.

The kernel doesn't have the luxury of the libc library to take care of the initial set up of various resources.

And hardware resources at any point even during application execution in turn call system calls which call I/O routines coded in assembly language.


A
Adison Masih

The .S extension indicates an assembly language file.

Why cant we have .C files instead?

Because the raw source code — be it human-entered or compiler-generated — is assembly, not C.


佚名

They are assembler code files:

An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices in which each statement corresponds to a single machine language instruction. An assembly language is specific to a certain computer architecture, in contrast to most high-level programming languages, which may be more portable.

and so these files are not the same as C code files.

Note that C files can be inlined with assembly instructions.


R
Raunak

.S files are assembly language files. They are a form of machine code. It is at a low level of programming. All machine dependent code is written in assembly language.The assembly language is different for different processors.


s
srikant-ritolia

.S files are code written in assembly language i.e low level of programming. In linux kernel source code, .S are generally the starting files which the kernel runs when the linux kernel starts booting (for eg.:- head.S) . We write this in .S and not in .C because we are not ready to run .C compiled .O file at this time. We need to work with architecture dependent registers and start the kernel.


F
Frederick

For completeness sake: There is ANOTHER ".s" file, that is not assembly, but rather an "SREC" binary file (encoded in ASCII) used to program microcontrollers.

https://en.wikipedia.org/wiki/SREC_(file_format)

Upon opening such a file, you would see lines of text that looked like this:

S315000000500C9413110C94D510189518950C949710B0
S31500000060189518950C941010189518950C946A10FC
S31500000070189518950C9403131895189518951895B6
S315000000800C940B1218951895189518950C944912FE
S315000000900C943D101895189518951895189518955F