Live data from Hacker News

Writing C software without the standard library

weeb.ddns.net

151–160 of 188 posts

Re: Writing C software without the standard library

#151

A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but…

> The syscall/errno stuff has always seemed unusual, inelegant, and inefficient

And non-reentrant.

errno is usually defined as TLS (thread local storage).

It's a mess.

Re: Writing C software without the standard library

#152
If the asm was written a little more cleverly, the syscalls would avoid almost all moves, because the compiler'd put everything in place:

  _syscall5:
    mov %r9, %r10
  _syscall3:
    mov %rcx, %rax
    syscall
    ret
And then:

  extern unsigned long _syscall3(
    unsigned long, unsigned long,
    unsigned long, unsigned long);

  extern unsigned long _syscall5(
    unsigned long, unsigned long, unsigned long,
    unsigned long, unsigned long, unsigned long);

  #define syscall0(NUM)             _syscall3(0,0,0,NUM)
  #define syscall1(NUM,A)           _syscall3(A,0,0,NUM)
  #define syscall2(NUM,A,B)         _syscall3(A,B,0,NUM)
  #define syscall3(NUM,A,B,C)       _syscall3(A,B,C,NUM)
  #define syscall4(NUM,A,B,C,D)     _syscall5(A,B,C,NUM,0,D)
  #define syscall5(NUM,A,B,C,D,E)   _syscall5(A,B,C,NUM,E,D)

Re: Writing C software without the standard library

#153

Earlier quoted context omitted.

You're getting down-voted (Which is unnecessary IMO) but you're really not wrong. `gcc` itself provides `stdint.h`, it's not actually part of libc - or rather, you can use it without actually having a libc in place. Generally this is a good move, because you can always write a `stdint.h` replacement on arch's that don't have one, but on ones that do you're guaranteed to get the types correct.

I use stdint.h too, but I'm honestly curious if there is any common platform around today where one of the following asserts fails: int main() { assert(sizeof(signed char) == 1); assert(sizeof(short) == 2); assert(sizeof(int) == 4); assert(sizeof(long long) == 8); return 0; } I'm not interested in the language lawyering, because yes I know the standard provides more freedom to compilers. I just think those definition…

Well there's a few things to keep in mind. For one, `sizeof(char)`is guarenteed to be `1` - the standard simply says so. That doesn't mean that a `char` is 8-bits long, however. On certain systems, like TI DSP's, `char` is 16-bits long, and thus `sizeof(short)` is also 1. `sizeof(int)` may be either 1 or 2, I can't recall (both are standards compliant).

All that said, POSIX requires `CHAR_BIT == 8`, which then basically ensures what you wrote to be true. So if you're willing to target POSIX (or POSIX-supporting systems) then such a thing is perfectly fine.

The `stdint.h` types are still better though, IMO, because they make your intentions a lot more clear.

Re: Writing C software without the standard library

#154

Earlier quoted context omitted.

my netiquette says, more than about three lines of code should go in a pastebin anyway

Then in a few years when someone is reading old posts the links to old services are broken or the service is gone. If the code is central to the comment, why would you put it some place other than the comment? If it's not small enough you can collapse it inline and if it's so large that you don't want it with the comment then perhaps you should be rethinking what you are posting. tldr; practicality and longevity shou…

I agree and this was my original thought. For example Reddit, for most of its life, relied on imgurl for hosting images. Before the single place images constantly broke after services died. With imgurl being third party even it has led to broken images at times (though with significantly less frequency). Now reddit is doing their own.

Hacker News is very technical and code heavy. Seems to make sense to me that some may want to communicate / discuss code itself. I could even see it opening up more conversations like "this is my implementation of X; thoughts?" or "do this in any other language" challenges.

Re: Writing C software without the standard library

#156
post #151

A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but…

> The syscall/errno stuff has always seemed unusual, inelegant, and inefficient And non-reentrant. errno is usually defined as TLS (thread local storage). It's a mess.

Yes, if UNIX had supported multithreading from the start, there is no way they would have chosen "errno" as the solution.

Obvious in hindsight, not so much in the 1970s.

Re: Writing C software without the standard library

#157
post #151

Earlier quoted context omitted.

> The syscall/errno stuff has always seemed unusual, inelegant, and inefficient And non-reentrant. errno is usually defined as TLS (thread local storage). It's a mess.

Yes, if UNIX had supported multithreading from the start, there is no way they would have chosen "errno" as the solution. Obvious in hindsight, not so much in the 1970s.

Somehow VMS managed to avoid that same mistake in the 70s.

Re: Writing C software without the standard library

#158
post #142

Earlier quoted context omitted.

He spoke about the space savings of the binary itself (which must be debug things and statically linked code). There is no sharing of that , unless multiple processes run that same binary.

Unless the binary size exceeds VM page size this has no real value even in the unshared case. Because the OS is still going to have to map a full page for the executable to be loaded into. That said techniques to allow a binary to fit under page size do have value; as the unused extra page can be used for other things.

The OS may map a full page for the binary but I'd expect that the time to load the binary from disk to memory would be saved. And once in memory the program would certainly take up fewer cache lines when executed.

Re: Writing C software without the standard library

#159
post #157

Earlier quoted context omitted.

Yes, if UNIX had supported multithreading from the start, there is no way they would have chosen "errno" as the solution. Obvious in hindsight, not so much in the 1970s.

Somehow VMS managed to avoid that same mistake in the 70s.

I keep hearing that a lot!

VMS sounds amazing, but somehow I'm still thinking this might be nostalgia.

Re: Writing C software without the standard library

#160

Interesting - my McAfee web washer blocked this site. Don't know why.

A lot of web/internet filters block ASM related content. My company uses Barracuda Networks filter and most ASM references/content are blocked, Reason: Hacking

They're not technically wrong... it's just 'hacking' in the traditional black magic/voodoo manipulation of actual systems components sense. That is, literally taking a hacksaw to a circuit board and altering it or making a new board.
Post reply on HN