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
Merge remote-tracking branch 'upstream/master' into i2c