toolchain/pack-sections: fix knapsack

Two bugs:

- do not overwrite argument `w' with loop variable
- check whether section fits to avoid negative array indices

Discovered-by: Derek Mulcahy